This question already has an answer here:
这个问题在这里已有答案:
- How to get whole and decimal part of a number? 16 answers
- What's the best way to get the fractional part of a float in PHP? 10 answers
如何获得一个数字的整数和小数? 16个答案
在PHP中获取float的小数部分的最佳方法是什么? 10个答案
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