当我有时将(int)与(double)一起使用时,它的工作不正确
看看PHP代码示例:
我需要留下2个小数并删除其他
我知道数字格式();函数,但我不能使用它。因为它是舍入数
number_format(24.299,2);
输出:24.30
我需要:24.29
<?php
$str="158.2";
echo (double)$str; // Output: 158.2
echo (double)$str*100; // Output: 15820
echo (int)((double)$str*100); // Output: 15819 <-WHY? It Must To Be 15820, Why 15819?
echo ((int)((double)$str*100)/100); // Output: 158.19
?>
我需要在数字中保留两位小数,并去掉另一位小数而不取整。
由于浮点精度(例如,请参阅以下问题:PHP-浮点精度),158.2 * 100
不是确切地说是15820
,而是类似于15819.99999999
。
现在(int)
用于类型转换,不用于舍入,并且点之后的任何数字都被截断。
我需要在数字中保留两位小数,并去掉另一位小数而不取整。
这很简单:
number_format($str, 2);
更新
number_format
进行循环,所以它有点复杂:
bcmul($str,100,0)/100
bcmul
以任意精度相乘,在这种情况下为0。结果:
bcmul(158.2,100,0)/100 == 158.2
bcmul(24.299,100,0)/100 == 24.29
这并不能回答为什么会发生这种情况(可能是精度错误),但要解决您的问题,请尝试使用$foo = sprintf("%.2f", (float)$str);
。
示例:
$str = "158.2";
$num = (double)$str;
print sprintf("%.2f", $num);
编辑:事实上,是的,这是一个精度问题。(在C++中)通过打印158.2到20个小数位,我得到了"158.1999999998863132"的输出。这是浮点/双精度值的固有问题。在PHP中使用echo sprintf("%.20f", $var);
可以看到同样的效果。
首先,PHP是一种允许您键入杂耍的语言。这意味着你不需要(int)
或(double)
来做你想做的事情。
<?php
$str="158.2"; //could also do $str = 158.2
echo $str; // Ouput: 158.2
echo $str * 100; //Output: 15820
echo number_format($str, 2); //Output: 158.20
echo number_format(($str*100)/100, 2); //Output: 158.20
?>
使用number_format
命令可以根据需要设置数字格式。
更多此处
永远不要将未知分数强制转换为整数,请参阅http://www.php.net/manual/en/language.types.integer.php.(int) ( (0.1+0.7) * 10 );
将产生7,而不是人们所期望的8。从float到integer的强制转换将始终向下取整,您可能还需要检查运算符的优先级http://php.net/manual/en/language.operators.precedence.php.
解决方案:在投射之前计算分数。$fStr = (float) $str; $iStr = (int) $fStr;
已修复
function cutDecimals($number,$decimal){
$_str=(string)$number;
if(strpos($_str,".")!==false){
$dotPosition=strpos($_str,".")+1;
$_numCount=strpos($_str,".");
$_decimal=strlen($_str)-$dotPosition;
if($_decimal<$decimal) return (double)$_str;
else return (double)substr($_str,0,$_numCount+$decimal+1);
}else return (double)$_str;
}
echo cutDecimals("158.099909865",2)."<br />";
echo cutDecimals("14.02",2)."<br />";
echo cutDecimals("41.12566",2)."<br />";
echo cutDecimals("1.981",2)."<br />";
echo cutDecimals("0.4111",2)."<br />";
echo cutDecimals("144.2",2)."<br />";
echo cutDecimals("55.000000",2)."<br />";
echo cutDecimals("1456115.499811445121",2)."<br />";
?>