How to get whole and decimal part of a number?

前端 未结 16 1230
名媛妹妹
名媛妹妹 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 03:54

    If you can count on it always having 2 decimal places, you can just use a string operation:

    $decimal = 1.25;
    substr($decimal,-2);  // returns "25" as a string
    

    No idea of performance but for my simple case this was much better...

提交回复
热议问题