Created
March 6, 2009 22:08
-
-
Save lsmith/75096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes a native JavaScript Number and formats to string for display to user. | |
* | |
* @method format | |
* @param nData {Number} Number. | |
* @param oConfig {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> | |
* <dt>negativeFormat</dt> | |
* <dd>String used as a guide for how to indicate negative numbers. The first '#' character in the string will be replaced by the number. Default '-#'.</dd> | |
* </dl> | |
* @return {String} Formatted number for display. Note, the following values | |
* return as "": null, undefined, NaN, "". | |
*/ | |
YAHOO.util.Number.format = function (n,cfg) { | |
if (!isFinite(+n)) { | |
return ''; | |
} | |
n = !isFinite(+n) ? 0 : +n; | |
cfg = YAHOO.lang.merge(YAHOO.util.Number.format.defaults, (cfg || {})); | |
var neg = n < 0, absN = Math.abs(n), | |
places = cfg.decimalPlaces, | |
sep = cfg.thousandsSeparator, | |
s, bits, i; | |
if (places < 0) { | |
// Get rid of the decimal info | |
s = absN - (absN % 1) + ''; | |
i = s.length + places; | |
// avoid 123 vs decimalPlaces -4 (should return "0") | |
if (i > 0) { | |
// leverage toFixed by making 123 => 0.123 for the rounding | |
// operation, then add the appropriate number of zeros back on | |
s = Number('.' + s).toFixed(i).slice(2) + | |
new Array(s.length - i + 1).join('0'); | |
} else { | |
s = "0"; | |
} | |
} else { // There is a bug in IE's toFixed implementation: | |
// for n in {(-0.94, -0.5], [0.5, 0.94)} n.toFixed() returns 0 | |
// instead of -1 and 1. Manually handle that case. | |
s = absN < 1 && absN >= 0.5 && !places ? '1' : absN.toFixed(places); | |
} | |
if (absN > 1000) { | |
bits = s.split(/\D/); | |
i = bits[0].length % 3 || 3; | |
bits[0] = bits[0].slice(0,i) + | |
bits[0].slice(i).replace(/(\d{3})/g, sep + '$1'); | |
s = bits.join(cfg.decimalSeparator); | |
} | |
s = cfg.prefix + s + cfg.suffix; | |
return neg ? cfg.negativeFormat.replace(/#/,s) : s; | |
}; | |
YAHOO.util.Number.format.defaults = { | |
decimalSeparator : '.', | |
decimalPlaces : null, | |
thousandsSeparator : ',', | |
prefix : '', | |
suffix : '', | |
negativeFormat : '-#' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment