2
I’m performing some calculations in the Front-End to not need to request for the Back-End.
But I came across the following problem in the calculations.
In the JS:
490 * 60 / 60 = 490
490 / 60 * 60 = 489.99999999999994
In PHP:
490 * 60 / 60 = 490
490 / 60 * 60 = 490
- Because this happens in JS and PHP does not?
PHP only displays 490, but it’s actually the same value as JS. Just use
number_format
to increase the decimal places:number_format(490/60*60, 14)
. And this is due to the representation of the numbers with floating point, already discussed here on the site.– Woss
I’ll read about it and try to understand it better, because this is "new" to me.
– Don't Panic
What Every Programmer Should Know About Floating-Point Arithmetic
– Woss