Why not use float?
See original GitHub issueThis is a legitimate question. I’m doing research on financial math, and I don’t really understand why you’re using integers, when you can use floating point numbers.
Yes, floating point numbers are not guaranteed to be exact, but integers also have limitations. Since you tend to default to a precision of 2 digits, I can’t think of a (reasonable size) calculation where you can just use a floating point number and round with toFixed()
for display.
I hope this is not taken as a criticism, I’m just trying to learn. I wrote some notes on my blog but I would love to understand if I’m incorrect.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Why You Should Never Use Float and Double for Monetary ...
Float and double are bad for financial (even for military use) world, never use them for monetary calculations. If precision is one of...
Read more >Why not use Double or Float to represent currency?
The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power...
Read more >Floating Point Numbers & Currency Rounding Errors
Because floats and doubles cannot accurately represent the base 10 multiples we use for money, so it is impossible to represent 0.1 (or...
Read more >Never Use Floats for Money
Why floats are bad for money. We would think yeah, floats would be great for money, because $1.40 is 1 dollar and 4...
Read more >For values in C# and C, why don't you just always use floats?
When choosing a data type, it's best to choose a type that adequately represents the anticipated data. Using a float to represent a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a great question! I’ll try to summarize as best I can.
Integers (or some other representation such as Decimals, or BigNumber) always have 100% precision. There is no precision loss outside of the limitations of
Number.MAX_SAFE_INTEGER
if you’re using integers; other methods may have other restrictions. If currencies always had an exact precision of 2, you would be correct thattoFixed(2)
likely would work in those cases.Unfortunately, currencies don’t always end up with a precise number and that’s where things can start to cause trouble. Taxes and other percentage calculations happen with a result that ends up with a number less than
.01
, so we have to decide how to round. Since a floating point number can end up higher or lower than the real value, usingtoFixed(2)
can actually round in the wrong direction, leading to missing or added cents.Let me explain with an example:
Both numbers should be exactly the same (
17.955
), but they actually aren’t in reality since floating point precision can’t accurately store every decimal number. Sometimes the same number can be rounded up, or rounded down depending on how that number was eventually reached. If the number beyond the precision is.004999999~
or less, it’ll round down to.00
. If it’s larger than that it’ll round up. It doesn’t really matter where that number is, so even a number as small as.00000000000000001
can cause the final precision to be off by a cent if it’s on the wrong side of the half cent.And while the default precision is
2
, the precision doesn’t really matter since we deal in integers. We can safely deal in whatever precision you would like (again up toNumber.MAX_SAFE_INTEGER
).Some fixed point numbers (specifically the fractional portion) cannot be stored in binary floats without issues.
Take for example:
Over numerous computations this error can grow larger and larger when doing currency math.
A good article on Javascript and currecy is https://frontstuff.io/how-to-handle-monetary-values-in-javascript