php 如何四舍五入到最接近的千位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5150001/
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 16:57:14  来源:igfitidea点击:

How to round to nearest Thousand?

php

提问by Steven Hammons

How Do I round a number to its nearest thousand?

如何将数字四舍五入到最接近的千位?

function round($var) {
    // Round it
}

回答by paxdiablo

PHP allows negative precision for roundsuch as with:

PHP 允许负精度,round例如:

$x = round ($x, -3); // Uses default mode of PHP_ROUND_HALF_UP.

Whereas a positive precision indicates where to round afterthe decimal point, negative precisions provide the same power beforethe decimal point. So:

正精度表示小数点四舍五入的位置,负精度在小数点提供相同的幂。所以:

n    round(1111.1111,n)
==   ==================
 3   1111.111
 2   1111.11
 1   1111.1
 0   1111
-1   1110
-2   1100
-3   1000

As a general solution, even for languages that don't have it built in, you simply do something like:

作为通用解决方案,即使对于没有内置它的语言,您只需执行以下操作:

  • add 500.
  • divide it by 1000(and truncate to integer if necessary).
  • multiply by 1000.
  • 添加500.
  • 除以1000(并在必要时截断为整数)。
  • 乘以1000

This is, of course, assuming you wantthe PHP_ROUND_HALF_UPbehaviour. There are some who think that bankers rounding, PHP_ROUND_HALF_EVEN, is better for reducing cumulative errors but that's a topic for a different question.

当然,这是假设您想要这种PHP_ROUND_HALF_UP行为。有些人认为银行家四舍五入 ,PHP_ROUND_HALF_EVEN更有助于减少累积错误,但这是另一个问题的主题。

回答by Amber

回答by Icode4food

Use the round functionas mentioned by other posters round($number, -3).

使用其他海报提到的圆形功能round($number, -3)

You can also, divide your number by 1,000, round to nearest whole number then multiply by 1,000.

您也可以将您的数字除以 1,000,四舍五入到最接近的整数,然后乘以 1,000。

Also, if you want to round up, you can divide by 1,000, negate the quotient, coerce it to an integer, negate it again and then multiply it by 1,000.

另外,如果要四舍五入,可以除以 1,000,将商取反,将其强制为整数,再次取反,然后再乘以 1,000。

回答by Theodore R. Smith

Here's an answer to round up to the next thousand:

以下是四舍五入到下一个千位的答案:

abs(round(($a - 500), -3)) . "\n";

abs(round(($a - 500), -3)) . "\n";

500-999 then round to 0 and 1000-1999 round to 1000, etc.

500-999 然后舍入到 0 和 1000-1999 舍入到 1000,依此类推。

If you want it to start at 1000, then just do

如果你想让它从 1000 开始,那就做

abs(round(($a + 500), -3)) . "\n";

abs(round(($a + 500), -3)) . "\n";

Cheers!

干杯!

回答by Mikel

For positive integers:

对于正整数:

function round($var) {
    return ($var + 500) / 1000 * 1000;
}

回答by Alexander Yancharuk

Just for your information simple calculation from Mikel answeris faster than round():

仅供参考,Mikel 答案的简单计算比round()快:

Test name       Repeats         Result          Performance     
calculation     10000           0.030229 sec    +0.00%
round           10000           0.040981 sec    -35.57%

Test source here.

测试源在这里

回答by Michel Ayres

Just a small change, may help to round up!

只是一个小小的改变,可能有助于围捕!

$x = ceil(220.20 / 1000) * 1000;
echo $x;