What's the best way to get the fractional part of a float in PHP?

前端 未结 10 920
不思量自难忘°
不思量自难忘° 2020-11-30 05:45

How would you find the fractional part of a floating point number in PHP?

For example, if I have the value 1.25, I want to return 0.25.

10条回答
  •  天命终不由人
    2020-11-30 06:13

    Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:

    function getDecimalPart($floatNum) {
        return abs($floatNum - intval($floatNum));
    }
    
    $decimalPart = getDecimalPart($floatNum);
    

提交回复
热议问题