简体   繁体   English

在PHP中舍入到最接近的5或9

[英]Round to nearest 5 or 9 in PHP

I need to round pricing of products to nearest 5 or 9 with a PHP function. 我需要使用PHP函数将产品的价格四舍五入到最接近的5或9

For example, it must always round up: 例如,它必须总是四舍五入:

$243 would round up to $245

$247 would round up to $249

Currently using the following code, but this only rounds to nearest 5: 当前使用以下代码,但这仅舍入到最接近的5:

function round_five($num) {
    return round($num*2,-1)/2;
}

Any advice would be greatly appreciated <3 任何建议将不胜感激<3

By checking whether the remainder of division by 10 is less than 5, we round it to 5, otherwise if it is greater than 5, we round it to 9. If not, the remainder is 5, and we return the number itself. 通过检查除以10的余数是否小于5,我们将其舍入为5,否则,将其舍入为5,将其舍入为9。如果不是,则余数为5,然后返回数字本身。

function my_round($number) {
    $inumber = ceil($number);

    $mod_10 = $inumber % 10;
    $mod_5 = $inumber % 5;

    if ($mod_10 < 5) {
        return $inumber + 5 - $mod_5;
    }

    if ($mod_10 > 5) {
        return $inumber + 10 - $mod_10 - 1;
    }

    return $inumber;
}

echo my_round(243) . PHP_EOL;
echo my_round(247) . PHP_EOL;
echo my_round(5) . PHP_EOL;
echo my_round(9) . PHP_EOL;
echo my_round(10) . PHP_EOL;
echo my_round(0) . PHP_EOL;
echo my_round(5.1) . PHP_EOL;
echo my_round(8.9) . PHP_EOL;
echo my_round(9.1) . PHP_EOL;

Outputs: 输出:

245
249
5
9
15
5
9
9
15

Requirements are a little vague, so it may still be tweaked to handle various other corner cases. 要求有点含糊,因此可能仍需进行调整以处理各种其他极端情况。

This is a bit, non mathy but should work: 这有点不算数,但应该可以:

function round9($n) {
    $n = ceil($n);
    $r = substr($n,-1) > 5 ? 9 : 5;
    $n = substr($n,0,-1) . $r;
    return $n;
}
  • first round up to nearest integer 第一舍入到最接近的整数
  • get last digit 获取最后一位
  • replace last digit with 9 if it's greater than 5. otherwise replace with a 5. 如果大于5则用9代替最后一位。否则用5代替。

UPDATE: I thought this was an interesting problem and now that we've got three correct answers which solve it from very different angles I thought I'd do a bit of performance analysis for my own curiosity. 更新:我认为这是一个有趣的问题,现在我们有了三个正确的答案,可以从非常不同的角度解决这个问题,我想我会为自己的好奇心进行一些性能分析。 neuro_sys's answer wins by far! Neuro_sys的答案已获胜! Here are the results in seconds to complete 10 million iterations: 以下是完成一千万次迭代所需的秒数结果:

time to build array: 1.7170281410217
round9: 10.753921985626
my_round: 1.6339750289917
rounder: 16.578145980835

Test was run on an 8GB, 4 Core Linode VPS running Ubuntu 16.04 / PHP 7.0.14 测试在运行Ubuntu 16.04 / PHP 7.0.14的8GB,4核心Linode VPS上运行

Related questions you should have read: 您应该阅读的相关问题:

This is the solution: 这是解决方案:

Because your issue is not using core mathemtics (5 and 9 are arbitary) or large indicies (largest count is 5 digits, 0,1,2,3,4) , simply count the numbers up: 因为您的问题不是使用核心数学(5和9是任意数字)或大指数(最大计数是5位数字,即0、1、2、3、4),所以只需将数字加起来即可:

<?php
function rounder($n) {
    $x=[5,9];
    $zz = ceil($n);
     $b = substr((string)$zz, -1); 
    while( !in_array($b,$x) ){

        $zz++;     
        $b = substr((string)$zz, -1);
    }

    return $zz;
}

$array[] = 243;
$array[] = 247;
$array[] = 249;
$array[] = 240;
$array[] = 250;

foreach($array as $row){

     print rounder($row)."<Br>";

}
unset($row);

======================== ========================

Output: 输出:

245 245
249 249
249 249
245 245
255 255

This can probably be shortened down to a few lines of code..... but whatever 可以将其缩短为几行代码.....但是无论如何

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM