T
Tuxedo
Does anyone know how to round up fraction values in Javascript?
I would like to modify a variable starting at 1 ending at 0 in 25
iterations, using setInterval, updating the variable accordingly. In other
words that should be 4% each step. If each interval runs every 50
milliseconds it should take about 1.25 seconds to complete the cycle.
interval = setInterval("change()",50);
1.00 <!- starting value
0.96 first iteration
0.92 second ...
0.88 third ..
0.84 etc.
0.80
0.76
0.72
0.68
0.64
0.60
0.56
0.52
0.48
0.44
0.40
0.36
0.32
0.28
0.24
0.20
0.16
0.12
0.08
0.04
0.00 <- end value and clearInterval(interval)
However, except for the first iteration, simply subtracting 0.04 from each
value will return nemeric values as follows:
0.96
0.9199999999999999
0.8799999999999999
0.8399999999999999
0.7999999999999998
0.7599999999999998
0.7199999999999998
0.6799999999999997
0.6399999999999997
0.5999999999999996
0.5599999999999996
0.5199999999999996
0.4799999999999996
0.4399999999999996
0.39999999999999963
0.35999999999999965
0.3199999999999997
0.2799999999999997
0.23999999999999969
0.19999999999999968
0.15999999999999967
0.11999999999999966
0.07999999999999965
0.039999999999999654
-3.469446951953614e-16
Any ideas? Thanks in advance!
I would like to modify a variable starting at 1 ending at 0 in 25
iterations, using setInterval, updating the variable accordingly. In other
words that should be 4% each step. If each interval runs every 50
milliseconds it should take about 1.25 seconds to complete the cycle.
interval = setInterval("change()",50);
1.00 <!- starting value
0.96 first iteration
0.92 second ...
0.88 third ..
0.84 etc.
0.80
0.76
0.72
0.68
0.64
0.60
0.56
0.52
0.48
0.44
0.40
0.36
0.32
0.28
0.24
0.20
0.16
0.12
0.08
0.04
0.00 <- end value and clearInterval(interval)
However, except for the first iteration, simply subtracting 0.04 from each
value will return nemeric values as follows:
0.96
0.9199999999999999
0.8799999999999999
0.8399999999999999
0.7999999999999998
0.7599999999999998
0.7199999999999998
0.6799999999999997
0.6399999999999997
0.5999999999999996
0.5599999999999996
0.5199999999999996
0.4799999999999996
0.4399999999999996
0.39999999999999963
0.35999999999999965
0.3199999999999997
0.2799999999999997
0.23999999999999969
0.19999999999999968
0.15999999999999967
0.11999999999999966
0.07999999999999965
0.039999999999999654
-3.469446951953614e-16
Any ideas? Thanks in advance!