如何将数字四舍五入到最接近的 10?
php 中如何将数字四舍五入到最接近的 10?
假设我有 23
,我将使用什么代码将其四舍五入为 30
?
How can we round off a number to the nearest 10 in php?
Say I have 23
, what code would I use to round it off to 30
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
floor()
将会下降。ceil()
将会上升。默认情况下,
round()
将转到最近的值。除以 10,进行 ceil,然后乘以 10 以减少有效数字。
编辑:我已经这样做了很长时间..但 TallGreenTree 的答案更干净。
floor()
will go down.ceil()
will go up.round()
will go to nearest by default.Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.
Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.
这会将 $number 舍入到最接近的 10。如果需要更改舍入模式,您还可以传递第三个变量。
更多信息在这里: http://php.net/manual/en/function.round.php
This will round $number to the nearest 10. You can also pass a third variable if necessary to change the rounding mode.
More info here: http://php.net/manual/en/function.round.php
我实际上正在寻找一个可以四舍五入到最近的变量的函数,并且这个页面不断出现在我的搜索中。因此,当我最终自己编写了该函数时,我想我会将其发布在这里供其他人查找。
该函数将舍入到最接近的变量:
此代码:
将输出:
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:
This code:
Will output:
这个问题的答案有很多,也许每个答案都会给你你想要的答案。但正如 @TallGreenTree 提到的,有一个函数可以实现这一点。
但@TallGreenTree 的答案的问题是它不会向上舍入,而是四舍五入到最接近的 10。要解决此问题,请在您的数字中添加 +5 以便向上舍入。如果要向下舍入,请执行
-5
。因此,在代码中:
您不能使用
舍入模式
进行舍入,因为它只舍入分数而不是整数。如果您想四舍五入到最接近的
100
,则应使用+50
。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.
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
+5
to your number in order to round up. If you want to round down, do-5
.So in code:
You can't use the
round mode
for rounding up, because that only rounds up fractions and not whole numbers.If you want to round up to the nearest
100
, you shoud use+50
.div 除以 10,然后使用 ceil,然后乘以 10
http://php.net/manual/en /function.ceil.php
div by 10 then use ceil then mult by 10
http://php.net/manual/en/function.ceil.php
我们可以通过 round 进行“欺骗”
我们还可以避免使用浮点除法 编辑
:我不知道(并且网站上没有详细记录)
round
现在支持“负”精度,这样就可以更方便的再次使用Edit:如果总是想向上取整,可以尝试
We can "cheat" via round with
We can also avoid going through floating point division with
Edit: I didn't know (and it's not well documented on the site) that
round
now supports "negative" precision, so you can more easily useEdit again: If you always want to round up, you can try
到最接近的 10 ,应如下
to nearest 10 , should be as below
尝试
round(23, -1);
Try
round(23, -1);
我想四舍五入到最大数字位的下一个数字(有名字吗?),所以我做了以下函数(在 php 中):
I wanted to round up to the next number in the largest digits place (is there a name for that?), so I made the following function (in php):
试试这个:
Try this:
我的第一个冲动是在谷歌上搜索“php math”,我发现有一个名为“round()”的核心数学库函数,这可能就是您想要的。
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.
对于那些想要使用原始 SQL 而不使用 php、java、python 等的人来说。
设置 SQL_SAFE_UPDATES = 0;
UPDATE db.table SET value=ceil(value/10)*10 其中值不像“%0”;
For people who want to do it with raw SQL, without using php, java, python etc.
SET SQL_SAFE_UPDATES = 0;
UPDATE db.table SET value=ceil(value/10)*10 where value not like '%0';
这可以使用 PHP 'fmod' 函数轻松完成。下面的代码特定于 10,但您可以将其更改为任何数字。
输出:100
This can be easily accomplished using PHP 'fmod' function. The code below is specific to 10 but you can change it to any number.
OUTPUT: 100
试试这个......传入要四舍五入的数字,它会四舍五入到最接近的十分位。希望它有帮助......
round($num, 1);
Try this......pass in the number to be rounded off and it will round off to the nearest tenth.hope it helps....
round($num, 1);