diff options
| author | Braden Shepherdson | 2012-10-19 14:29:37 -0400 |
|---|---|---|
| committer | Misko Hevery | 2012-10-26 08:51:28 -0700 |
| commit | bca1604c12262b66ce3b8004994fb4841fb8b87d (patch) | |
| tree | 1adda119e2bb1b55b72b0764f5821ff414eb9faa /src | |
| parent | f4517b500c0d2357d89e8c889f32f1466e5c1612 (diff) | |
| download | angular.js-bca1604c12262b66ce3b8004994fb4841fb8b87d.tar.bz2 | |
fix(currency): Handle not-quite-zero values
IEEE 754 floating point sometimes results in values that are very small,
rather than zero. One example is 1.0 + 1.07 - 2.07, which returns
4.440892098500626e-16 instead of 0.
This change tweaks the number formatting logic so that an exponential
value with a negative exponent that is larger than the precision+1
returns 0 instead. For example: with precision 2, anything with an
exponent of -4, -5 or more would become 0. 9e-3 = 0.009 = 0.01, but 9e-4
= 0.0009 = 0.001 = 0.00. This detail is unlikely to matter since this
quirk is usually only triggered with values very close to zero.
Closes #1469
Diffstat (limited to 'src')
| -rw-r--r-- | src/ng/filter/filters.js | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 376afd85..57186981 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -117,9 +117,18 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) { formatedText = '', parts = []; + var hasExponent = false; if (numStr.indexOf('e') !== -1) { - formatedText = numStr; - } else { + var match = numStr.match(/([\d\.]+)e(-?)(\d+)/); + if (match && match[2] == '-' && match[3] > fractionSize + 1) { + numStr = '0'; + } else { + formatedText = numStr; + hasExponent = true; + } + } + + if (!hasExponent) { var fractionLen = (numStr.split(DECIMAL_SEP)[1] || '').length; // determine fractionSize if it is not specified |
