php 如何将数字四舍五入到最接近的 10?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1619265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:17:06  来源:igfitidea点击:

How to round up a number to nearest 10?

phprounding

提问by tarnfeld

How can we round off a number to the nearest 10 in php?

我们如何在 php 中将数字四舍五入到最接近的 10?

Say I have 23, what code would I use to round it off to 30?

说我有23,我会用什么代码来四舍五入30

回答by Daren Schwenke

floor()will go down.

floor()会下去。

ceil()will go up.

ceil()会涨。

round()will go to nearest by default.

round()默认情况下会去最近的。

Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

除以 10,做 ceil,然后乘以 10 以减少有效数字。

$number = ceil($input / 10) * 10;

Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.

编辑:我已经这样做了很长时间了..但 TallGreenTree 的答案更清晰。

回答by TallGreenTree

round($number, -1);

This will round $number to the nearest 10. You can also pass a third variable if necessary to change the rounding mode.

这会将 $number 舍入到最接近的 10。如果需要,您还可以传递第三个变量来更改舍入模式。

More info here: http://php.net/manual/en/function.round.php

更多信息在这里:http: //php.net/manual/en/function.round.php

回答by Kenny

I was actually searching for a function that could round to the nearest variable, and this page kept coming up in my searches. So when I finally ended up writing the function myself, I thought I would post it here for others to find.

我实际上是在寻找一个可以四舍五入到最近变量的函数,这个页面一直在我的搜索中出现。所以当我最终自己写了这个函数的时候,我想我会把它贴在这里让其他人找到。

The function will round to the nearest variable:

该函数将四舍五入到最近的变量:

function roundToTheNearestAnything($value, $roundTo)
{
    $mod = $value%$roundTo;
    return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
}

This code:

这段代码:

echo roundToTheNearestAnything(1234, 10).'<br>';
echo roundToTheNearestAnything(1234, 5).'<br>';
echo roundToTheNearestAnything(1234, 15).'<br>';
echo roundToTheNearestAnything(1234, 167).'<br>';

Will output:

将输出:

1230
1235
1230
1169

回答by Timo002

There are many anwers in this question, probably all will give you the answer you are looking for. But as @TallGreenTree mentions, there is a function for this.

这个问题有很多答案,可能都会给你你正在寻找的答案。但正如@TallGreenTree 提到的,有一个功能。

But the problem of the answer of @TallGreenTree is that it doesn't round up, it rounds to the nearest 10. To solve this, add +5to your number in order to round up. If you want to round down, do -5.

但是@TallGreenTree 的答案的问题是它没有四舍五入,而是四舍五入到最接近的 10。要解决这个问题,请添加+5到您的数字以四舍五入。如果你想四舍五入,做-5

So in code:

所以在代码中:

round($num + 5, -1);

You can't use the round modefor rounding up, because that only rounds up fractions and not whole numbers.

您不能使用round modefor 四舍五入,因为它只四舍五入分数而不是整数。

If you want to round up to the nearest 100, you shoud use +50.

如果你想四舍五入到最近的100,你应该使用+50.

回答by Johnno Nolan

div by 10 then use ceil then mult by 10

div 乘以 10 然后使用 ceil 然后乘以 10

http://php.net/manual/en/function.ceil.php

http://php.net/manual/en/function.ceil.php

回答by Adam Wright

We can "cheat" via round with

我们可以通过回合“欺骗”

$rounded = round($roundee / 10) * 10;

We can also avoid going through floating point division with

我们还可以避免通过浮点除法

function roundToTen($roundee)
{
  $r = $roundee % 10;
  return ($r <= 5) : $roundee - $r : $roundee + (10 - $r);
}

Edit: I didn't know (and it's not well documented on the site) that roundnow supports "negative" precision, so you can more easily use

编辑:我不知道(并且网站上没有详细记录)round现在支持“负”精度,因此您可以更轻松地使用

$round = round($roundee, -1);

Edit again: If you always want to round up, you can try

再次编辑:如果你总是想四舍五入,你可以试试

function roundUpToTen($roundee)
{
  $r = $roundee % 10;
  if ($r == 0)
    return $roundee;
  return $roundee + 10 - $r;    
}

回答by Artyom Sokolov

Try

尝试

round(23, -1);

round(23, -1);

回答by wormhit

$value = 23;
$rounded_value = $value - ($value % 10 - 10);
//$rounded_value is now 30

回答by tarnfeld

Try this:

尝试这个:

ceil($roundee / 10) * 10;

回答by Berry

My first impulse was to google for "php math" and I discovered that there's a core math library function called "round()" that likely is what you want.

我的第一个冲动是在 google 上搜索“php math”,我发现有一个名为“round()”的核心数学库函数,这可能正是您想要的。