[英]Round up Price in php
I want to round up prices in PHP我想在 PHP 中对价格进行四舍五入
eg I have the price £74.31
I want to round to the closest 74.35
or if above £74.36
then £74.40
and the decimals I always want to end with 5
and 0
.例如,我有价格
£74.31
我想四舍五入到最接近的74.35
或者如果高于£74.36
那么£74.40
和小数我总是想以5
和0
结尾。
For example例如
£74.31 would be round up to £74.35
£74.32 would be round up to £74.35
£74.33 would be round up to £74.35
£74.34 would be round up to £74.35
£74.35 would be round up to £74.35
£74.36 would be round up to £74.40
£74.37 would be round up to £74.40
£74.38 would be round up to £74.40
£74.39 would be round up to £74.40
£74.40 would be round up to £74.40
I attempted to round up prices with the below我试图用以下四舍五入价格
round(($price * $rate) * 2, 1)/2;
You can also try with this logic,你也可以试试这个逻辑,
<?php
$x = 74.31;
$number = $x*100;
if($number % 5){
$number = $number + 5 - ($number % 5);
}else{
$number = $number - ($number % 5);
}
$number /=100;
echo number_format((float)$number, 2, '.', '');
?>
Live demo : https://eval.in/748595现场演示: https : //eval.in/748595
Hopefully this makes sense, but if it doesn't some quick background:希望这是有道理的,但如果它没有一些快速的背景:
The round($number * 2) / 2
you posted works because there are two halves in any whole.您发布的
round($number * 2) / 2
有效,因为任何整体都有两半。 If you wanted to round to quarters you would use round($number * 4)/4
, and fifths would be round($number * 5)/5
.如果你想四舍五入,你可以使用
round($number * 4)/4
,五分之一将是round($number * 5)/5
。 etc, etc.等等等等。
Because it appears as if you want to round to the nearest nickel, there are 20 nickels in a dollar.因为看起来好像你想四舍五入到最接近的镍,所以一美元有 20 镍。 Problem is that it is willing to move itself up/down.
问题是它愿意向上/向下移动。 The same thing happens using
ceil()
, but it will only move upwards, which appears to be what you want.使用
ceil()
会发生同样的事情,但它只会向上移动,这似乎是您想要的。
function round_to_nickel($item, $decimals=false){
$num = ceil($item * 20) / 20;
return ($decimals) ? number_format($num, (int)$decimals) : $num;
}
根据您的要求使用 php floor()
和ceil()
和round()
函数。
ceil()
, floor()
, and round()
alone will not produce the correct/desired result;单独使用
ceil()
、 floor()
和round()
不会产生正确/所需的结果; additional calculations are required.需要额外的计算。
After multiplying the input by 20
then subtracting .1
, then call ceil()
, finally divide by 20
.将输入乘以
20
然后减去.1
,然后调用ceil()
,最后除以20
。 By effect, numbers originally ending in 5 (in the hundredths place) will not be rounded up, but actually remain unchanged.实际上,最初以 5(百分位)结尾的数字不会四舍五入,但实际上保持不变。
For clean, concise formatting of the new float value, I recommend printf()
with a format parameter that demands two decimal places in the output.为了干净、简洁地格式化新的浮点值,我建议
printf()
使用格式参数,要求输出中有两位小数。
printf('£%.02f', $num, ceil($num * 20 - .1) / 20)
Unit Tests : ( Demo )单元测试:(演示)
for ($num = 74.30; $num < 74.41; $num += .01) {
var_export(sprintf('%.02f => £%.02f', $num, ceil($num * 20 - .1) / 20));
echo "\n";
}
Output:输出:
'74.30 => £74.30'
'74.31 => £74.35'
'74.32 => £74.35'
'74.33 => £74.35'
'74.34 => £74.35'
'74.35 => £74.35' #not rounded up to 74.40
'74.36 => £74.40'
'74.37 => £74.40'
'74.38 => £74.40'
'74.39 => £74.40'
'74.40 => £74.40'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.