Discuss Scratch
- Discussion Forums
- » Bugs and Glitches
- » Repeating decimal problem
- Choppead
-
Scratcher
5 posts
Repeating decimal problem
SO…
When I try to add/subtract 2 decimals
And it doesn't only just happen in the editor, but in the project aswell.
This is a problem for clone-based counters like the one I'm using, so please fix this bug or give me some workarounds.
When I try to add/subtract 2 decimals
((0.7) + (0.2))You think that the answer would be 0.9, but for some reason it just ends up being a very long string of decimals (in this case being 0.899999…)
And it doesn't only just happen in the editor, but in the project aswell.
This is a problem for clone-based counters like the one I'm using, so please fix this bug or give me some workarounds.

- Knightbot63
-
Scratcher
1000+ posts
Repeating decimal problem
This is some sort of JavaScript float bug I believe. The only way to fix this is using the round operator unless you're using the decimals.
- Knightbot63
-
Scratcher
1000+ posts
Repeating decimal problem
It's actuallyThat returns 1… Not 0.9. To get 0.9, you need to remove the multiply and divide operators.(round (((0.7) + (0.2)) * (10))) / (10)
Edit: Javascript floats are very finicky and it returns 1 no matter what. Closest result is without the round block…
Last edited by Knightbot63 (Feb. 7, 2024 02:37:39)
- medians
-
Scratcher
1000+ posts
Repeating decimal problem
I can't replicate, did you multiply by 10 inside the round?It's actuallyThat returns 1… Not 0.9. To get 0.9, you need to remove the multiply and divide operators.(round (((0.7) + (0.2)) * (10))) / (10)
Edit: Javascript floats are very finicky and it returns 1 no matter what. Closest result is without the round block…
- PhiPhenomenon
-
Scratcher
500+ posts
Repeating decimal problem
This is some sort of JavaScript float bug I believe.That's right, it's because numbers are represented in binary.
Here's a more in-depth explanation. Computers currently use the binary number system to represent numbers. This is pretty easy to do with whole numbers:

However, with numbers with decimals, it might not be possible to represent them in a way that doesn't repeat in binary. Because computers have a limited amount of memory, they have to make an approximation.

Usually, the approximation is close to what the actual number is, but it usually won't be perfect.
- Discussion Forums
- » Bugs and Glitches
-
» Repeating decimal problem