Let's say I have some currency value const num = '2.05';
.
I would like to receive this amount in cents. So the possible solution:
const num = '2.05';
const floatNum = parseFloat(num); // 2.05
floatNum * 100; // 204.99999999999997
The problem here in rounding, expected value 205
not 204.99999999999997
.
What I can do to resolve this:
const num = '2.05';
return parseInt(num.replace('.', '')); // 205
It will work in most cases but I have doubt that it's fully safe variant. Would be great if someone suggest better method.