datatype-number-debug.js revision 0e3dbf948a3123fbb50c8344f99bb52465e5defa
YUI.add('datatype-number', function(Y) {
/**
* The DataType utility provides a set of utility functions to operate on native
* JavaScript data types.
*
* @module datatype
*/
var LANG = Y.Lang,
/**
* Number submodule.
*
* @class DataType.Number
* @static
*/
Number = {
/**
* Returns string name.
*
* @method toString
* @return {String} String representation for this object.
*/
toString: function() {
return "DataType.Number";
},
/**
* Converts data to type Number.
*
* @method parse
* @param data {String | Number | Boolean} Data to convert. The following
* values return as null: null, undefined, NaN, "".
* @return {Number} A number, or null.
* @static
*/
parse: function(data) {
var number = (data === null) ? data : +data;
if(LANG.isNumber(number)) {
return number;
}
else {
Y.log("Could not parse data " + Y.dump(data) + " to type Number", "warn", Number.toString());
return null;
}
},
/**
* Takes a Number and formats to string for display to user.
*
* @method format
* @param data {Number} Number.
* @param config {Object} (Optional) Optional configuration values:
* <dl>
* <dt>prefix {String}</dd>
* <dd>String prepended before each number, like a currency designator "$"</dd>
* <dt>decimalPlaces {Number}</dd>
* <dd>Number of decimal places to round.</dd>
* <dt>decimalSeparator {String}</dd>
* <dd>Decimal separator</dd>
* <dt>thousandsSeparator {String}</dd>
* <dd>Thousands separator</dd>
* <dt>suffix {String}</dd>
* <dd>String appended after each number, like " items" (note the space)</dd>
* </dl>
* @return {String} Formatted number for display. Note, the following values
* return as "": null, undefined, NaN, "".
*/
format: function(data, config) {
data = LANG.isNumber(data) ? data : Number.parse(data);
if(LANG.isNumber(data)) {
config = config || {};
var isNeg = (data < 0),
output = data + "",
decPlaces = config.decimalPlaces,
decSep = config.decimalSeparator || ".",
thouSep = config.thousandsSeparator,
decIndex,
newOutput, count, i;
// Decimal precision
if(LANG.isNumber(decPlaces)) {
// Round to the correct decimal place
output = data.toFixed(decPlaces);
}
// Decimal separator
if(decSep !== "."){
output = output.replace(".", decSep);
}
// Add the thousands separator
if(thouSep) {
// Find the dot or where it would be
decIndex = output.lastIndexOf(decSep);
decIndex = (decIndex > -1) ? decIndex : output.length;
// Start with the dot and everything to the right
newOutput = output.substring(decIndex);
// Working left, every third time add a separator, every time add a digit
for (count = 0, i=decIndex; i>0; i--) {
if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
newOutput = thouSep + newOutput;
}
newOutput = output.charAt(i-1) + newOutput;
count++;
}
output = newOutput;
}
// Prepend prefix
output = (config.prefix) ? config.prefix + output : output;
// Append suffix
output = (config.suffix) ? output + config.suffix : output;
return output;
}
// Still not a Number, just return unaltered
else {
return data;
}
},
formatOrig: function(data, config) {
data = LANG.isNumber(data) ? data : Number.parse(data);
if(LANG.isNumber(data)) {
config = config || {};
var isNeg = (data < 0),
output = data + "",
decPlaces = config.decimalPlaces,
decSep = config.decimalSeparator,
thouSep = config.thousandsSeparator,
dotIndex;
// Decimal precision
if(LANG.isNumber(decPlaces)) {
// Round to the correct decimal place
output = data.toFixed(decPlaces);
}
// Decimal separator
if(decSep){
output = output.replace(".", decSep);
}
// Add the thousands separator
if(thouSep) {
dotIndex = output.lastIndexOf(decSep);
dotIndex = (dotIndex > -1) ? dotIndex : output.length;
var newOutput = output.substring(dotIndex);
var count = -1;
for (var i=dotIndex; i>0; i--) {
count++;
if ((count%3 === 0) && (i !== dotIndex) && (!isNeg || (i > 1))) {
newOutput = thouSep + newOutput;
}
newOutput = output.charAt(i-1) + newOutput;
}
output = newOutput;
}
// Prepend prefix
output = (config.prefix) ? config.prefix + output : output;
// Append suffix
output = (config.suffix) ? output + config.suffix : output;
return output;
}
// Still not a Number, just return unaltered
else {
return data;
}
}
};
Y.namespace("DataType").Number = Number;
}, '@VERSION@' ,{requires:['??']});