Php 如何取整到非零阈值

Php 如何取整到非零阈值,php,Php,我需要将给定的价格四舍五入到非零阈值,例如9、8、3或5 我试过圆形、地板、天花板的方法,但没有得到我需要的结果 功能税汇总$number,$taxRate { $taxprice=ceil$number; $mod_10; $mod_5; $mod_3; $mod_8; 转换$税率{ 案例3: $delta=0.0000001; $mod_3=$taxprice%3; //$taxprice=$taxprice+$delta; 返回$taxprice+3-$mod_3; 打破 案例5: $mo

我需要将给定的价格四舍五入到非零阈值,例如9、8、3或5

我试过圆形、地板、天花板的方法,但没有得到我需要的结果

功能税汇总$number,$taxRate { $taxprice=ceil$number; $mod_10; $mod_5; $mod_3; $mod_8; 转换$税率{ 案例3: $delta=0.0000001; $mod_3=$taxprice%3; //$taxprice=$taxprice+$delta; 返回$taxprice+3-$mod_3; 打破 案例5: $mod_5=$taxprice%5; 返回$taxprice+5-$MODU 5; 打破 案例8: $mod_8=$taxprice%8; 返回$taxprice+8-$MODU 8; 打破 案例9: $mod_10=$taxprice%10; 返回$taxprice+10-$mod_10-1; 打破 违约: $mod_10=$taxprice%10; 返回$taxprice+10-$mod_10-1; 打破 } } 四舍五入至9欧元的示例: 15,00欧元将是19欧元 17.25欧元将为19欧元 1456,23欧元将为1459,00欧元
19,01欧元将是29,00欧元

您将“四舍五入”数字和10之间的差额添加到您的价格中,因此1代表9,2代表8,…,然后除以10。ceil将该值四舍五入到下一个整数,再乘以10,再减去10和“四舍五入”数字之间的差值:

$prices = [15, 17.25, 1456.23, 19.01];
$round_to = [9, 8, 3, 5];

foreach($round_to as $r) {
  foreach($prices as $price) {
    $rounded = ceil ( ( $price + 10 - $r ) / 10 ) * 10 - ( 10 - $r );
    echo $price . ' rounded up to next ' . $r . ' is '. $rounded . "\n";  
  }
  echo "\n";
}
输出:

15 rounded up to next 9 is 19
17.25 rounded up to next 9 is 19
1456.23 rounded up to next 9 is 1459
19.01 rounded up to next 9 is 29

15 rounded up to next 8 is 18
17.25 rounded up to next 8 is 18
1456.23 rounded up to next 8 is 1458
19.01 rounded up to next 8 is 28

15 rounded up to next 3 is 23
17.25 rounded up to next 3 is 23
1456.23 rounded up to next 3 is 1463
19.01 rounded up to next 3 is 23

15 rounded up to next 5 is 15
17.25 rounded up to next 5 is 25
1456.23 rounded up to next 5 is 1465
19.01 rounded up to next 5 is 25

9,8,3,5不,我的意思是,我根据开关情况使用这些数字,用户一次只输入一个数字,不使用逗号。就像在tax_roundup1500,9中一样,情况9将运行,给定的价格1500将用9进行取整