Calculation problem

Asked

Viewed 33 times

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

Jsfiddle

In PHP:

 490 * 60 / 60 = 490
 490 / 60 * 60 = 490

Phpsandbox

  • Because this happens in JS and PHP does not?
  • 1

    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.

  • I’ll read about it and try to understand it better, because this is "new" to me.

  • 2
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.