How to get whole and decimal part of a number?

前端 未结 16 1250
名媛妹妹
名媛妹妹 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:01

    Brad Christie's method is essentially correct but it can be written more concisely.

    function extractFraction ($value) 
    {
        $fraction   = $value - floor ($value);
        if ($value < 0)
        {
            $fraction *= -1;
        }
    
        return $fraction;
    }
    

    This is equivalent to his method but shorter and hopefully easier to understand as a result.

提交回复
热议问题