如何把一个数字四舍五入到最近的10?

时间:2022-12-08 23:21:14

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

我们怎样才能把一个数字四舍五入到最接近的10呢?

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

假设我有23,我用什么代码把它四舍五入到30?

14 个解决方案

#1


151  

floor() will go down.

地板()将会降低。

ceil() will go up.

装天花板()将上升。

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.

编辑:我这样做已经很久了。但泰格伦特里的回答更简洁。

#2


137  

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.

这将四舍五入到最近的10美元。如果需要更改舍入模式,还可以传递第三个变量。

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

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

#3


9  

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

#4


6  

div by 10 then use ceil then mult by 10

用10分然后用ceil然后用10分

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

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

#5


6  

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 +5 to 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 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.

如果你想凑到最近的100,你应该用+50。

#6


2  

Try

试一试

round(23, -1);

轮(23日1);

#7


2  

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 round now supports "negative" precision, so you can more easily use

编辑:我不知道(网站上也没有详细的记录),现在一轮支持“负”精度,所以你可以更容易地使用

$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;    
}

#8


2  

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

#9


1  

Try this:

试试这个:

ceil($roundee / 10) * 10;

#10


1  

Just round down to the nearest 10, and then add 10.

四舍五入到最近的10,然后加10。

round($num, -1) + 10

#11


0  

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.

我的第一个想法是用谷歌来表示“php数学”,我发现有一个叫做“round()”的核心数学库函数,这很可能是您想要的。

#12


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、java、python等使用原始SQL的人,设置SQL_SAFE_UPDATES = 0;更新数据库。表集合值=ceil(值/10)*10,值不像'%0';

#13


0  

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):

我想把下一个数字放在最大的数字(有名字吗?)

//Get the max value to use in a graph scale axis, 
//given the max value in the graph
function getMaxScale($maxVal) {
    $maxInt = ceil($maxVal);
    $numDigits = strlen((string)$maxInt)-1; //this makes 2150->3000 instead of 10000
    $dividend = pow(10,$numDigits);
    $maxScale= ceil($maxInt/ $dividend) * $dividend;
    return $maxScale;
}

#14


-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);

轮(num美元,1);

#1


151  

floor() will go down.

地板()将会降低。

ceil() will go up.

装天花板()将上升。

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.

编辑:我这样做已经很久了。但泰格伦特里的回答更简洁。

#2


137  

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.

这将四舍五入到最近的10美元。如果需要更改舍入模式,还可以传递第三个变量。

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

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

#3


9  

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

#4


6  

div by 10 then use ceil then mult by 10

用10分然后用ceil然后用10分

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

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

#5


6  

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 +5 to 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 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.

如果你想凑到最近的100,你应该用+50。

#6


2  

Try

试一试

round(23, -1);

轮(23日1);

#7


2  

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 round now supports "negative" precision, so you can more easily use

编辑:我不知道(网站上也没有详细的记录),现在一轮支持“负”精度,所以你可以更容易地使用

$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;    
}

#8


2  

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

#9


1  

Try this:

试试这个:

ceil($roundee / 10) * 10;

#10


1  

Just round down to the nearest 10, and then add 10.

四舍五入到最近的10,然后加10。

round($num, -1) + 10

#11


0  

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.

我的第一个想法是用谷歌来表示“php数学”,我发现有一个叫做“round()”的核心数学库函数,这很可能是您想要的。

#12


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、java、python等使用原始SQL的人,设置SQL_SAFE_UPDATES = 0;更新数据库。表集合值=ceil(值/10)*10,值不像'%0';

#13


0  

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):

我想把下一个数字放在最大的数字(有名字吗?)

//Get the max value to use in a graph scale axis, 
//given the max value in the graph
function getMaxScale($maxVal) {
    $maxInt = ceil($maxVal);
    $numDigits = strlen((string)$maxInt)-1; //this makes 2150->3000 instead of 10000
    $dividend = pow(10,$numDigits);
    $maxScale= ceil($maxInt/ $dividend) * $dividend;
    return $maxScale;
}

#14


-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);

轮(num美元,1);