检查float变量小数点后的数字[重复]

时间:2022-06-02 17:07:54

This question already has an answer here:

这个问题在这里已有答案:

I have variable which is a float number

我有变量,这是一个浮点数

$num = 10.5;

and I want to check the value after the decimal point

我想检查小数点后的值

$decimal = function($num);
echo $decimal // should be 5;

how can I do this?

我怎样才能做到这一点?

1 个解决方案

#1


You could do something like this:

你可以这样做:

function decimal($number){
    $num = explode('.', $number);
    return $num[1];
}

$decimal = decimal(10.5);
echo $decimal; // should be 5

#1


You could do something like this:

你可以这样做:

function decimal($number){
    $num = explode('.', $number);
    return $num[1];
}

$decimal = decimal(10.5);
echo $decimal; // should be 5