# Decoding Why 0.6 + 0.3 = 0.8999999999999999 in JS and How to Solve?

Jeeva Ramanathan on November 16, 2023

Understanding IEEE 754 Floating-Point Numbers and also exploring how 0.1+0.2 is 0.30000000000000004 step-by-step While working on an o...

Paweł bbkr Pabian

Great explanation of IEEE754.

However for all precision-sensitive calculations I recommend Raku, because it automatically does fraction rationals. So if you write 0.12824 it will be represented as 1603/12500 internally:

\$ raku -e 'say 0.12824.nude'
(1603 12500)

Fraction calculations will always be performed using integer numerator and denominator, therefore will always be precise:

\$ perl -E 'say 0.7 - 0.6 - 0.1'
-2.77555756156289e-17  # FAIL

\$ python -c 'print(0.7 - 0.6 - 0.1)'
-2.7755575615628914e-17 # FAIL

\$ raku -e 'say 0.7 - 0.6 - 0.1'
0 # CORRECT!

As a bonus it also handles big nums out of the box. Worth trying.

Jeeva Ramanathan

Lev Nahar • Edited

Great article and solution for an issue not a lot of modern programmers know about (especially problematic when dealing with finances). Just one thing - I thought it is worth mentioning that .toFixed() in Javascript is a purely cosmetic method, it should only be used for display purposes since (like you mentioned) it returns the string representation of the decimal.

Jeeva Ramanathan

Thanks for your comment! You're correct that .toFixed() is like a cosmetic method as it only affects the way the number is displayed, not its actual value.

tq-bit

I didn't know.toPrecision before. Thanks for sharing, you wrote a great article there

Jeeva Ramanathan

Thanks! Glad this helped you discover toPrecision().

Hassan

That was so awesome :)

Jeeva Ramanathan

Mike Stemle

Well done, friend.

Jeeva Ramanathan

Thanks, friend!

Jeeva Ramanathan • Edited

🙌🏻

CitronBrick

You can enable syntax highlighting in Dev.to using

triple_backtick javascript
code
triple_backtick

Jeeva Ramanathan

Yeah, Sure🙌🏻

Samir

One of the best in-depth technical articles I've read so far. I'm sure not many JS enthusiasts will read this or even care about the intricacies of it So I'm lucky to have read it!
Is it just JavaScript or other languages also use the same IEEE 754 standard?

Jeeva Ramanathan • Edited