How to get whole and decimal part of a number?

前端 未结 16 1207
名媛妹妹
名媛妹妹 2020-11-27 03:10

Given, say, 1.25 - how do I get \"1\" and .\"25\" parts of this number?

I need to check if the decimal part is .0, .25, .5, or .75.

16条回答
  •  再見小時候
    2020-11-27 04:02

    The floor() method doesn't work for negative numbers. This works every time:

    $num = 5.7;
    $whole = (int) $num;  // 5
    $frac  = $num - $whole;  // .7
    

    ...also works for negatives (same code, different number):

    $num = -5.7;
    $whole = (int) $num;  // -5
    $frac  = $num - $whole;  // -.7
    

提交回复
热议问题