Php 菲律宾圆及;天花板未正确解析数据
从xml文件中提取并分配给变量但输出不正确的数字。我不太清楚为什么或者如何避开它。这就是一个例子Php 菲律宾圆及;天花板未正确解析数据,php,xml,math,rounding,ceil,Php,Xml,Math,Rounding,Ceil,从xml文件中提取并分配给变量但输出不正确的数字。我不太清楚为什么或者如何避开它。这就是一个例子 $pricing_high = 1.15; echo $pricing_high; 这显然显示为1.15。但当我这样分配时: $price = ceil($pricing_high / 0.05) * 0.05; echo $price; 这将显示2 $price = round($pricing_high / 0.05) * 0.05; echo $price; 这将显示1 当像这样传递数字
$pricing_high = 1.15;
echo $pricing_high;
这显然显示为1.15。但当我这样分配时:
$price = ceil($pricing_high / 0.05) * 0.05;
echo $price;
这将显示2
$price = round($pricing_high / 0.05) * 0.05;
echo $price;
这将显示1
当像这样传递数字时,如何使数字正确地四舍五入到最接近的5美分?从PHP的round函数文档开始: 可以将精度指定为第二个参数:
$pricing_high = 1.15;
$price = round($pricing_high / 0.05, 2) * 0.05;
echo $price;
请注意第二个参数2的值,因为ceil()和floor()没有这个精度,您可以将结果相乘,然后再除以。是
1.15
=1美元15美分吗?如果是:
echo sprintf('%.2f', round($price / 0.05) * 0.05); // Rounds to nearest
echo sprintf('%.2f', ceil($price / 0.05) * 0.05); // Rounds up
测试:
$price = 1.13;
echo sprintf('%.2f', round($price / 0.05) * 0.05); // Outputs: 1.15
echo sprintf('%.2f', ceil($price / 0.05) * 0.05); // Outputs: 1.15
$price = 1.12;
echo sprintf('%.2f', round($price / 0.05) * 0.05); // Outputs: 1.10
echo sprintf('%.2f', ceil($price / 0.05) * 0.05); // Outputs: 1.15
如果
1.15
=1.15美分,则将0.05
替换为5
这两个都将输出1.15
。使其与$price=round($pricing\u high/5,2)*5一起工作;我怎样才能让它保持0?例如,它将显示1.00或从0.4到0.40,而不是显示1(我得到了:)$price1=sprintf(“%01.2f”,$price);谢谢你的信息!