PHP取余的那些事

1、百分号取余

$val=9.45; 
$result=$val*100;
echo intval($result);   //这里输出944
echo $result%100;    //这里输出44
echo fmod(floatval($result),100);  //这里输出45

解释:因为php默认对变量进行取整进行取余运算的

2、取余溢出

<?php
$num1 = 1494313163777;
$num2 = 9999;
//直接计算取余会出错,出现负数 -8779
echo $num1 % $num2;

//解决方案:
$num1 = floatval($num1);
$luck_num = fmod($num1, $num2);
//显示38正确
echo $luck_num;



Bookmark the permalink.

Comments are closed.