What is wrong with those javascript numbers ?
I'm trying to sum up earnings but js math calculation has some strange behaviour.
let earned = 0
setInterval(() => {
earned += 0.1
console.log(earned)
}, 1000)
The only one solution I came up for such problem, is to use round numbers instead of decimals, then divide them by 100 and use .toFixed(2)
to add decimals. But I'm not quite sure if its the best way for this type of issue.
Is there any existing best practice using in JS world or I'm doing something wrong ?
let earned = 0
setInterval(() => {
earned += 10
output = earned / 100
output = output.toFixed(2)
output = Number(output)
console.log(typeof output, output)
}, 1000)
And would I deal with 8 decimals such as 14.12345678
?