[英]Round last digit to 0 , 5 or 9 in PHP
I am looking to round the last digit of product prices in WooCommerce with a PHP function, based on 3 rules.我希望根据 3 条规则将 WooCommerce 中产品价格的最后一位数字舍入为 PHP function。 Price has to include cents and it does not matter if it gets rounded up or down.价格必须包括美分,向上或向下取整都没有关系。
If end between 0 and 4, change to 0
If end in 5, no change and stays at 5
If end between 6 and 9, change to 9
For example:例如:
A price of $23.12 will be rounded to $23.10
A price of 39.45 will be rounded to $39.45
A price of $4.26 will be rounded to $4.29
Currently using the following code but it only rounds to a whole number.目前使用以下代码,但它只四舍五入到一个整数。
function my_rounding_function( $price ) {
$price = round( $price );
return $price;
}
Any help would be very much appreciated: :)任何帮助将不胜感激::)
Try this.试试这个。 Using the BC math functions, this will guard against rounding errors.使用 BC 数学函数,这将防止舍入错误。 Updated to truncate (round) thousands first.更新为首先截断(舍入)千位。
Outputs:输出:
float(1)
float(1)
float(125.99)
float(12.3)
float(2.05)
float(3.69)
Code:代码:
<?php
function roundPerceptual(float $price):float
{
// Truncate to precision of 2 decimals.
$price = round($price, 2);
// Get last digit using MOD 10.
$lastDigit = bcmod($price * 100, 10);
// + casts the string to a number, compare to each threshold.
if (+$lastDigit < 5)
$newDigit = '0';
elseif (+$lastDigit > 5)
$newDigit = '9';
else
// Equal to 5.
$newDigit = '5';
// First drop the old hundredth, then add the new hundredth.
// Again, use + to cast to float.
return ($price - +"0.0$lastDigit") + +"0.0$newDigit";
}
$prices = [ 1, 1.04, 125.959, 12.337, 2.05, 3.67 ];
foreach ($prices as $price)
var_dump(roundPerceptual($price));
/*
Outputs:
float(1)
float(1)
float(125.99)
float(12.3)
float(2.05)
float(3.69)
*/
function my_rounding_function( $price ) {
$price = (string) $price;
if(strpos($price, '.'))
{
if($price[-1] < 5)
{
$price[-1] = 0;
}
elseif($price[-1] > 5)
{
$price[-1] = 9;
}
}
return (float) $price;
}
echo my_rounding_function(15) . '<br>';
echo my_rounding_function(15.3) . '<br>';
echo my_rounding_function(15.5) . '<br>';
echo my_rounding_function(15.8) . '<br>';
echo my_rounding_function(15.43) . '<br>';
echo my_rounding_function(12.45) . '<br>';
echo my_rounding_function(12.47) . '<br>';
// output
// 15
// 15
// 15.5
// 15.9
// 15.4
// 12.45
// 12.49
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.