开发者

How to get the division part of a number

开发者 https://www.devze.com 2023-03-29 19:12 出处:网络
Say I have 1.234I want to get the .234 I tried echo 1.234%1 //I get 0 I am rusty, what is the correc开发者_高级运维t way?

Say I have 1.234 I want to get the .234 I tried echo 1.234%1 //I get 0

I am rusty, what is the correc开发者_高级运维t way?

(The tags says PHP as this might be an issue only with PHP, but I really am looking for the general solution).


php's % modulo operator converts its arguments to integers. To get a floating-point modulus, use fmod:

echo fmod(1.234, 1);


You can remove the whole number from the number itself. in php its:

$num = 1.234;
echo $num - floor($num);


Subtract the integer portion of $x ((int)$x) from $x:

$x = 1.234;
$d = $x - (int)$x;
// $d now equals 0.234
echo $d;

Example


Just substract integer part 1.234 - (int)1.234


Try this:

echo 1.234 - intval(1.234);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号