6

I found a very strange issue, the issue is the ROUND method in PHP and Javascript the calculation results are not the same!?

See the following example:

PHP

echo round(175.5); // 176
echo round(-175.5); // -176

Javascript

console.log(Math.round(175.5)); // 176
console.log(Math.round(-175.5)); // -175 <-why not -176!!??

anyone know why? and how to make Javascript and PHP the same results?

Jasper
  • 2,314
  • 3
  • 26
  • 33
  • Why is this strange to you? – Jay Blanchard Mar 14 '17 at 16:12
  • [javascript vs php rounding](http://stackoverflow.com/questions/6437062/javascript-vs-php-rounding) and [floating point percision in javascript](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – Symeon Quimby Mar 14 '17 at 16:15
  • Plz check the PHP_ROUND_ constants http://it1.php.net/manual/en/math.constants.php and by the way javascript is not php. – JustOnUnderMillions Mar 14 '17 at 16:15
  • I think but i'm not sure that is a math different between languages ! In math, -175.5 will be rounded -175. –  Mar 14 '17 at 16:16
  • @Soheyl On math, It depends also on your given system what is possible http://stackoverflow.com/questions/8770992/int-max-size-for-32bit-system – JustOnUnderMillions Mar 14 '17 at 16:18
  • 1
    To begin with, [PHP round()](http://php.net/round) has a parameter with up to 4 algorithms to pick from. – Álvaro González Mar 14 '17 at 16:18
  • @JustOnUnderMillions thx for the link, I mean by math's logic and not a program, and I mean the JavaScript has more correct return than PHP in this example :) –  Mar 14 '17 at 16:25
  • @Soheyl Ok, but i was more point to on witch hardware runs software. https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers – JustOnUnderMillions Mar 14 '17 at 16:35

5 Answers5

5

That's not an issue, it is well documented

If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. Note that this differs from many languages' round() functions, which often round this case to the next integer away from zero, instead (giving a different result in the case of negative numbers with a fractional part of exactly 0.5).

If you want the same behaviour on Javascript, I would use

var n = -175.5;
var round = Math.round(Math.abs(n))*(-1)
DrKey
  • 3,365
  • 2
  • 29
  • 46
5

A quick solution is to do the following:

echo round(-175.5, 0, PHP_ROUND_HALF_DOWN); // -175

There are other modes to choose from:

  1. PHP_ROUND_HALF_UP - default
  2. PHP_ROUND_HALF_EVEN
  3. PHP_ROUND_HALF_ODD

See the documentation for more information.

This function will behave the same as in :

function jsround($float, $precision = 0){
  if($float < 0){
     return round($float, $precision, PHP_ROUND_HALF_DOWN);
  }

  return round($float, $precision);
}
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • Thank you for your answer, your answer is also very useful but can only choose one :(. – Jasper Mar 14 '17 at 16:39
  • No worries, you wanted the value of `-176` preferably, this only answers how to get the same value controlled from [tag:PHP]. – Xorifelse Mar 14 '17 at 16:42
2
console.log(Math.round(175.5)); // 176
console.log(Math.round(-175.5)); // -175 <-why not -176!!??

175.5 its round value 176 it's value increasing.

-175.5 round value is -175. Because when I round -175.5 then it also increasing that means -175>-176.

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
1

To control it more use ceil and floor for rounding. That way you can choose which way to round

Brian McCall
  • 1,831
  • 1
  • 18
  • 33
0

Or... if you wanted JavaScript to behave the same as PHP, use this:

function phpRound(number) {
  if(number < 0)
    return 0 - Math.round(0 - number);
  return Math.round(number);
}
dehart
  • 1,554
  • 15
  • 18