PHP ROUND vs Javascript ROUND

后端 未结 5 1024
花落未央
花落未央 2021-01-13 16:12

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

5条回答
  •  余生分开走
    2021-01-13 16:41

    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)
    

提交回复
热议问题