2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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;
44 }, '3.3.0' ,{requires:['yui-base']});
45 YUI.add('datatype-number-format', function(Y) {
51 * @submodule datatype-number
55 * Format number submodule.
58 * @submodule datatype-number-format
62 * DataType.Number provides a set of utility functions to operate against Number objects.
64 * @class DataType.Number
69 Y.mix(Y.namespace("DataType.Number"), {
71 * Takes a Number and formats to string for display to user.
74 * @param data {Number} Number.
75 * @param config {Object} (Optional) Optional configuration values:
77 * <dt>prefix {String}</dd>
78 * <dd>String prepended before each number, like a currency designator "$"</dd>
79 * <dt>decimalPlaces {Number}</dd>
80 * <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
81 * <dt>decimalSeparator {String}</dd>
82 * <dd>Decimal separator</dd>
83 * <dt>thousandsSeparator {String}</dd>
84 * <dd>Thousands separator</dd>
85 * <dt>suffix {String}</dd>
86 * <dd>String appended after each number, like " items" (note the space)</dd>
88 * @return {String} Formatted number for display. Note, the following values
89 * return as "": null, undefined, NaN, "".
91 format: function(data, config) {
92 if(LANG.isNumber(data)) {
93 config = config || {};
95 var isNeg = (data < 0),
97 decPlaces = config.decimalPlaces,
98 decSep = config.decimalSeparator || ".",
99 thouSep = config.thousandsSeparator,
104 if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
105 // Round to the correct decimal place
106 output = data.toFixed(decPlaces);
111 output = output.replace(".", decSep);
114 // Add the thousands separator
116 // Find the dot or where it would be
117 decIndex = output.lastIndexOf(decSep);
118 decIndex = (decIndex > -1) ? decIndex : output.length;
119 // Start with the dot and everything to the right
120 newOutput = output.substring(decIndex);
121 // Working left, every third time add a separator, every time add a digit
122 for (count = 0, i=decIndex; i>0; i--) {
123 if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
124 newOutput = thouSep + newOutput;
126 newOutput = output.charAt(i-1) + newOutput;
133 output = (config.prefix) ? config.prefix + output : output;
136 output = (config.suffix) ? output + config.suffix : output;
140 // Not a Number, just return as string
142 return (LANG.isValue(data) && data.toString) ? data.toString() : "";
148 }, '3.3.0' ,{requires:['yui-base']});
151 YUI.add('datatype-number', function(Y){}, '3.3.0' ,{use:['datatype-number-parse', 'datatype-number-format']});