API Docs for: 3.18.1
Show:

File: number/js/number-format.js

  1. /**
  2. * The Number Utility provides type-conversion and string-formatting
  3. * convenience methods for Numbers.
  4. *
  5. * @module datatype-number
  6. * @main datatype-number
  7. */
  8.  
  9. /**
  10. * Format number submodule.
  11. *
  12. * @module datatype-number
  13. * @submodule datatype-number-format
  14. */
  15.  
  16. /**
  17. * Number provides a set of utility functions to operate against Number objects.
  18. *
  19. * @class Number
  20. * @static
  21. */
  22. var LANG = Y.Lang;
  23.  
  24. Y.mix(Y.namespace("Number"), {
  25. /**
  26. * Takes a Number and formats to string for display to user.
  27. *
  28. * @method format
  29. * @param data {Number} Number.
  30. * @param config {Object} (Optional) Optional configuration values:
  31. * <dl>
  32. * <dt>prefix {String}</dd>
  33. * <dd>String prepended before each number, like a currency designator "$"</dd>
  34. * <dt>decimalPlaces {Number}</dd>
  35. * <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
  36. * <dt>decimalSeparator {String}</dd>
  37. * <dd>Decimal separator</dd>
  38. * <dt>thousandsSeparator {String}</dd>
  39. * <dd>Thousands separator</dd>
  40. * <dt>suffix {String}</dd>
  41. * <dd>String appended after each number, like " items" (note the space)</dd>
  42. * </dl>
  43. * @return {String} Formatted number for display. Note, the following values
  44. * return as "": null, undefined, NaN, "".
  45. */
  46. format: function(data, config) {
  47. if(LANG.isNumber(data)) {
  48. config = config || {};
  49.  
  50. var isNeg = (data < 0),
  51. output = data + "",
  52. decPlaces = config.decimalPlaces,
  53. decSep = config.decimalSeparator || ".",
  54. thouSep = config.thousandsSeparator,
  55. decIndex,
  56. newOutput, count, i;
  57.  
  58. // Decimal precision
  59. if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
  60. // Round to the correct decimal place
  61. output = data.toFixed(decPlaces);
  62. }
  63.  
  64. // Decimal separator
  65. if(decSep !== "."){
  66. output = output.replace(".", decSep);
  67. }
  68.  
  69. // Add the thousands separator
  70. if(thouSep) {
  71. // Find the dot or where it would be
  72. decIndex = output.lastIndexOf(decSep);
  73. decIndex = (decIndex > -1) ? decIndex : output.length;
  74. // Start with the dot and everything to the right
  75. newOutput = output.substring(decIndex);
  76. // Working left, every third time add a separator, every time add a digit
  77. for (count = 0, i=decIndex; i>0; i--) {
  78. if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
  79. newOutput = thouSep + newOutput;
  80. }
  81. newOutput = output.charAt(i-1) + newOutput;
  82. count++;
  83. }
  84. output = newOutput;
  85. }
  86.  
  87. // Prepend prefix
  88. output = (config.prefix) ? config.prefix + output : output;
  89.  
  90. // Append suffix
  91. output = (config.suffix) ? output + config.suffix : output;
  92.  
  93. return output;
  94. }
  95. // Not a Number, just return as string
  96. else {
  97. Y.log("Could not format data from type Number", "warn", "number");
  98. return (LANG.isValue(data) && data.toString) ? data.toString() : "";
  99. }
  100. }
  101. });
  102.  
  103. Y.namespace("DataType");
  104. Y.DataType.Number = Y.Number;
  105.