Requires

Provides

Number.Extras.js

Extends the Number type object to include useful methods to work with numbers.

License:
MIT-style license
Authors:
Arian Stolwijk
  1. 26
  2. 27
  3. 28
Number.implement({ format: function(options){

Thanks dojo and YUI for some inspiration

  1. 30
  2. 31
  3. 32
  4. 33
  5. 34
  6. 35
  7. 36
  8. 37
  9. 38
  10. 39
  11. 40
  12. 41
  13. 42
  14. 43
  15. 44
  16. 45
  17. 46
  18. 47
  19. 48
  20. 49
  21. 50
  22. 51
  23. 52
  24. 53
  25. 54
  26. 55
  27. 56
  28. 57
  29. 58
  30. 59
  31. 60
  32. 61
  33. 62
  34. 63
  35. 64
  36. 65
  37. 66
  38. 67
  39. 68
  40. 69
  41. 70
  42. 71
  43. 72
  44. 73
  45. 74
  46. 75
  47. 76
  48. 77
  49. 78
  50. 79
  51. 80
  52. 81
  53. 82
  54. 83
  55. 84
  56. 85
  57. 86
  58. 87
  59. 88
  60. 89
  61. 90
  62. 91
  63. 92
  64. 93
var value = this, locale = Object.clone(Locale.get('Number')); options = Object.merge(locale, options || {}); var negative = value < 0, decimal = options.decimal, precision = options.precision, group = options.group, decimals = options.decimals; if (negative){ if (options.prefix) options.negative.prefix = options.prefix + options.negative.prefix; options = Object.merge(options, options.negative); value = -value; } if (decimals > 0 && decimals <= 20) value = value.toFixed(decimals); if (precision >= 1 && precision <= 21) value = value.toPrecision(precision); value += ''; if (options.scientific === false){ var match = value.match(/^(.+)e\+(\d)$/i); if (match){ value = match[1].replace('.', ''); var length = match[2] - match[1].split('.').getLast().length; while (length--) value += '0'; } } if (decimal != '.') value = value.replace('.', decimal); if (options.group){ var index = value.lastIndexOf(decimal); index = (index > -1) ? index : value.length; var newOutput = value.substring(index), i = index; while (i--){ if ((index - i - 1) % 3 == 0 && i != (index - 1)) newOutput = group + newOutput; newOutput = value.charAt(i) + newOutput; } value = newOutput; } if (options.prefix) value = options.prefix + value; if (options.suffix) value += options.suffix; return value; }, formatCurrency: function(){ return this.format(Locale.get('Number.currency')); }, formatPercentage: function(){ return this.format(Locale.get('Number.percentage')); } });