php中的数字格式和舍入

php中的数字格式和舍入,php,Php,我需要点后面的以下格式 xx.01 and xx.02 go to xx.00 xx.03 and xx.04 go to xx.05 xx.06 and xx.07 go to xx.05 xx.08 and xx.09 go to xx.10 xx.11 and xx.12 go to xx.10 xx.13 and xx.14 go to xx.15 有人能给我一个PHP函数,将点后的数字转换成期望值吗 0.05 / 0.10 / 0.15/ 0.20 / 0.25 / 0.30 / 0

我需要点后面的以下格式

xx.01 and xx.02 go to xx.00
xx.03 and xx.04 go to xx.05
xx.06 and xx.07 go to xx.05
xx.08 and xx.09 go to xx.10
xx.11 and xx.12 go to xx.10
xx.13 and xx.14 go to xx.15
有人能给我一个PHP函数,将点后的数字转换成期望值吗

0.05 / 0.10 / 0.15/ 0.20 / 0.25 / 0.30 / 0.35 / 0.40 etc….
如您所述,此循环没有默认的第二个参数


i、 e
soRound(1.07)
返回需要舍入的
1.05
,但只能使用
round
舍入到最接近的十分之一。你想转到最近的二十号。解决方案是乘以2,四舍五入到最接近的十分之一,除以2,然后根据需要格式化:

function soRound($a, $to=0.05) {
  return round($a / $to) * $to ;
}
输出:

$data = [0, 0.01, 0.07, 0.09, 1.56, 1.73, 3.14159];

foreach ($data as $num) {
    $num = round($num * 2, 1) / 2;
    echo number_format($num, 2) . "\n";
}

函数形式:

0.00
0.00
0.05
0.10
1.55
1.75
3.15

你试过什么或做过一些研究吗?round(),你想舍入吗我使用了round()函数,但没有得到我想要的正确结果。有任何逻辑,然后请让我知道我会建立@Wasim当系统告诉你一个标题问题已经存在时,解决方法是写一个更好的标题,而不是把“number”拼错为“numbar”。另外,真的,你试过什么?非常感谢。可能是从1.68到1.70so 1.70,而不是1.7?使用
number\格式($number,2)
function roundToNearest05($num) {
    return round($num * 2, 1) / 2;
}

// or, more generically, this:

function roundTo($num = 0, $nearest = 0.05) {
    return round($num / $nearest) * $nearest;
}