How to divide numbers without remainder in PHP?

前端 未结 6 1878
抹茶落季
抹茶落季 2020-12-29 18:08

How does one divide numbers but exclude the remainder in PHP?

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 18:37

    For most practical purposes, the accepted answer is correct.

    However, for builds where integers are 64 bits wide, not all possible integer values are representable as a double-precision float; See my comment on the accepted answer for details.

    A variation of

    $n = ($i - $i % $m) / $m;
    

    (code taken from KingCrunch's comment under another answer) will avoid this problem depending on the desired rounding behavior (bear in mind that the result of the modulus operator may be negative).

提交回复
热议问题