2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('datatype-number-parse', function(Y) {
11 * Parse number submodule.
14 * @submodule datatype-number-parse
15 * @for DataType.Number
20 Y.mix(Y.namespace("DataType.Number"), {
22 * Converts data to type Number.
25 * @param data {String | Number | Boolean} Data to convert. The following
26 * values return as null: null, undefined, NaN, "".
27 * @return {Number} A number, or null.
29 parse: function(data) {
30 var number = (data === null) ? data : +data;
31 if(LANG.isNumber(number)) {
40 // Add Parsers shortcut
41 Y.namespace("Parsers").number = Y.DataType.Number.parse;
47 YUI.add('datatype-number-format', function(Y) {
53 * @submodule datatype-number
57 * Format number submodule.
60 * @submodule datatype-number-format
64 * DataType.Number provides a set of utility functions to operate against Number objects.
66 * @class DataType.Number
71 Y.mix(Y.namespace("DataType.Number"), {
73 * Takes a Number and formats to string for display to user.
76 * @param data {Number} Number.
77 * @param config {Object} (Optional) Optional configuration values:
79 * <dt>prefix {String}</dd>
80 * <dd>String prepended before each number, like a currency designator "$"</dd>
81 * <dt>decimalPlaces {Number}</dd>
82 * <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
83 * <dt>decimalSeparator {String}</dd>
84 * <dd>Decimal separator</dd>
85 * <dt>thousandsSeparator {String}</dd>
86 * <dd>Thousands separator</dd>
87 * <dt>suffix {String}</dd>
88 * <dd>String appended after each number, like " items" (note the space)</dd>
90 * @return {String} Formatted number for display. Note, the following values
91 * return as "": null, undefined, NaN, "".
93 format: function(data, config) {
94 if(LANG.isNumber(data)) {
95 config = config || {};
97 var isNeg = (data < 0),
99 decPlaces = config.decimalPlaces,
100 decSep = config.decimalSeparator || ".",
101 thouSep = config.thousandsSeparator,
106 if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
107 // Round to the correct decimal place
108 output = data.toFixed(decPlaces);
113 output = output.replace(".", decSep);
116 // Add the thousands separator
118 // Find the dot or where it would be
119 decIndex = output.lastIndexOf(decSep);
120 decIndex = (decIndex > -1) ? decIndex : output.length;
121 // Start with the dot and everything to the right
122 newOutput = output.substring(decIndex);
123 // Working left, every third time add a separator, every time add a digit
124 for (count = 0, i=decIndex; i>0; i--) {
125 if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
126 newOutput = thouSep + newOutput;
128 newOutput = output.charAt(i-1) + newOutput;
135 output = (config.prefix) ? config.prefix + output : output;
138 output = (config.suffix) ? output + config.suffix : output;
142 // Not a Number, just return as string
144 return (LANG.isValue(data) && data.toString) ? data.toString() : "";
155 YUI.add('datatype-number', function(Y){}, '3.0.0' ,{use:['datatype-number-parse', 'datatype-number-format']});