gZanyLvl 2
So I'm trying to make a timer that subtracts only 0.1 every 100 milliseconds but it shows a trail of random 0's and 9's. How do I get it so that it only shows the first 2 numbers?
My Code:
So I'm trying to make a timer that subtracts only 0.1 every 100 milliseconds but it shows a trail of random 0's and 9's. How do I get it so that it only shows the first 2 numbers?
My Code:
I would like to see the full code along with it not being in blocks because it is very unreadable
you could do String(Timer).substring(0, 2)
to show the front two numbers
gZany
might be a floating point error considering you're doing a repeated addition of -0.1
just put something like
timer = round(timer*100)/100
to get rid of all decimals but the first two
use Number.toFixed(<point>) to get it back to normal
for example (0.60000000001).toFixed(1) returns "0.6" (as a string)
Letti42 I first learned about that function while trying to make a digital stopwatch as well, it's been useful ever since.
Also the reason you get the .9999999 or .0000004 is because floating point math cannot represent certain values as exact binary values. Kinda like how 1/3 is 0.3333333 in decimal.
Try 0.1+0.2 in the console to see.