最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHP ROUND vs Javascript ROUND - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 14, 2017 at 16:13 Jasper asked Mar 14, 2017 at 16:12 JasperJasper 2,4043 gold badges27 silver badges34 bronze badges 8
  • Why is this strange to you? – Jay Blanchard Commented Mar 14, 2017 at 16:12
  • javascript vs php rounding and floating point percision in javascript – Symeon Quimby Commented Mar 14, 2017 at 16:15
  • Plz check the PHP_ROUND_ constants it1.php/manual/en/math.constants.php and by the way javascript is not php. – JustOnUnderMillions Commented Mar 14, 2017 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. – user5820249 Commented Mar 14, 2017 at 16:16
  • 1 To begin with, PHP round() has a parameter with up to 4 algorithms to pick from. – Álvaro González Commented Mar 14, 2017 at 16:18
 |  Show 3 more ments

5 Answers 5

Reset to default 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)

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 javascript:

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

  return round($float, $precision);
}
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.

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

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);
}
发布评论

评论列表(0)

  1. 暂无评论