Javascript Math - number divided by its multiple results in a .99999999999999 float

Hi,
I’m working my way through final FCC JavaScript projects and finally encountered an error that I don’t know how to google.
I’m trying to find how many coins there are in an amount of money with this formula, however, the result always ends up as a crazy float

cid[i][2] = cid[i][1] / coinsArray[i]
console.log(typeof(cid[1][1]) + " " + cid[1][1])
console.log(typeof(coinsArray[1]) + " " + coinsArray[1])
console.log(cid[i][2] = cid[i][1] / coinsArray[i]);

number 2.05
number 0.05
40.99999999999999

This is how floating point values work

I would try using an integer number of cents instead.

1 Like

While I don’t really get why floating point values work this way with some values but not with others, I may be starting to understand the running joke about data types in JavaScript :slight_smile:
Thank you for the advice

You can take a look at this video for more explanation on floating point maths

1 Like

Floating point is stored base 2 instead of base 10, so you don’t get a prefect representation of base 10 numbers.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.