Created
November 17, 2009 18:55
-
-
Save lsmith/237151 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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); | |
| } | |
| bits = s.split(/\D/); | |
| if (absN > 1000) { | |
| i = bits[0].length % 3 || 3; | |
| bits[0] = bits[0].slice(0,i) + | |
| bits[0].slice(i).replace(/(\d{3})/g, sep + '$1'); | |
| } | |
| s = cfg.prefix + bits.join(cfg.decimalSeparator) + cfg.suffix; | |
| return neg ? cfg.negativeFormat.replace(/#/,s) : s; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment