I'm writing an app for myself. I need to handle money and currencies.
I read this and I want to try the second approach:
You can multiply floats into integers before you calculate, then divide them back.
(0.2 * 100 + 0.01 * 100) / 100 // returns 0.21
It’s a fine solution but requires extra calculations either on object construction or on each manipulation. This isn’t necessarily draining on performances, but still more process work than necessary.
With my backend I store values in int64
.
QUESTION
Let's take 1.1
.
1.1 * 100 = 110.00000000000001
!!!
How can I be safe for all operations?
It's safe to always use (1.1 * 100).toFixed()
?