Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Currency filter doesn't truncate floats correctly when the JS float math is wacky. #1469

Closed
mattsgarrison opened this issue Oct 17, 2012 · 0 comments

Comments

@mattsgarrison
Copy link

JS floating point math goes awry when you do 1.07 + 1 - 2.07, it returns 4.440892098500626e-16, whereas 1.00 + 1 - 2.00 returns 0.

However, when you use that same floating point math in an angular expression with a currency filter, the filter does not truncate/convert the scientific notation to $0.00.

For reference, this fiddle will display the problem: http://jsfiddle.net/mattsgarrison/vZh69/ if you provide the first input 1.07, the second input 1, and the third input 2.07.

bshepherdson added a commit to bshepherdson/angular.js that referenced this issue Oct 19, 2012
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 angular#1469
IgorMinar pushed a commit that referenced this issue Oct 30, 2012
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants