如何确保浮点数总是使用PHP进行四舍五入?

时间:2021-09-13 08:40:02

I want to make sure a float in PHP is rounded up if any decimal is present, without worrying about mathematical rounding rules. This function would work as follows:

我希望确保在PHP中,如果存在任何小数,就对浮点数进行四舍五入,而不必担心数学舍入规则。该函数的工作如下:

1.1 to 2
1.2 to 2
1.9 to 2
2.3 to 3
2.8 to 3

I know the round() function exists but I don't see any function for rounding up if any decimal is found. Is there any easy way to do this?

我知道圆形()函数存在,但如果找到任何小数,则看不到任何函数。有什么简单的方法吗?

6 个解决方案

#1


59  

Use the ceil function:

使用装天花板函数:

$number = ceil(1.1); //2

#2


16  

I know this is an old topic, however it appears in Google. I will extend Blake Plumb's answer regarding precision.

我知道这是一个老话题,但它出现在谷歌。我将扩展布莱克·普拉博关于精度的回答。

ceil(1024.321 * 100) / 100;

Multiplying by 100 and dividing by 100 only works with one-hundredths. This isn't accurate on tenths, one-thousandths, one-hundred thousandths, etc.

乘以100除以100只对百分之一有效。这不是十分之一,千分之一,千分之一等等。

function round_up($number, $precision = 2)
{
    $fig = pow(10, $precision);
    return (ceil($number * $fig) / $fig);
}

Results:

结果:

var_dump(round_up(1024.654321, 0)); // Output: float(1025)
var_dump(round_up(1024.654321, 1)); // Output: float(1024.7)
var_dump(round_up(1024.654321, 2)); // Output: float(1024.66)
var_dump(round_up(1024.654321, 3)); // Output: float(1024.655)
var_dump(round_up(1024.654321, 4)); // Output: float(1024.6544)
var_dump(round_up(1024.654321, 5)); // Output: float(1024.65433)
var_dump(round_up(1024.654321, 6)); // Output: float(1024.654321)

Notes:

注:

Thanks for the contributions from Joseph McDermott and brandom for improving my original snippet.

感谢Joseph McDermott和brandom对改进我的原始代码片段的贡献。

#3


10  

Ceil will do that for you

西儿会帮你的

Taken from the example:

从示例:

<?php
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3
?>

#4


10  

I know this question has long since been answered, but it came up when I did a google search on the topic. If you want to round up with precision, then a good method would be to use the ceil function and times the number by how many decimal points you want to represent and then divide by that number.

我知道这个问题已经有很长一段时间了,但是当我在谷歌上搜索这个话题时,它出现了。如果你想要精确地四舍五入,那么一个好的方法就是使用ceil函数乘以你想要表示的小数点然后除以这个数字。

ceil(1024.321*100)/100

would produce 1024.33

会产生1024.33

#5


2  

I like Ash's response, although I would have:

我喜欢Ash的回答,尽管我可能会:

$fig = (int) str_pad('1', $precision + 1, '0');

Makes sense that if I provide precision '2', I would expect it rounded to 2 decimal places. Matter of choice though I suppose. Thanks for the answer Ash, works well.

如果我提供精确的'2',我希望它四舍五入到小数点后两位。不过我想这是一个选择的问题。谢谢你的回答,阿什,很好。

#6


2  

I like Ash's answer and Joseph's choice so I suggest to:

我喜欢艾什的回答和约瑟夫的选择,所以我建议:

$fig = pow(10, $precision);

It should work also with zero and negative precision values es:

它也应该工作为零和负精度值es:

var_dump(round_up(1024.654321, -1)); // Output: float(103)

#1


59  

Use the ceil function:

使用装天花板函数:

$number = ceil(1.1); //2

#2


16  

I know this is an old topic, however it appears in Google. I will extend Blake Plumb's answer regarding precision.

我知道这是一个老话题,但它出现在谷歌。我将扩展布莱克·普拉博关于精度的回答。

ceil(1024.321 * 100) / 100;

Multiplying by 100 and dividing by 100 only works with one-hundredths. This isn't accurate on tenths, one-thousandths, one-hundred thousandths, etc.

乘以100除以100只对百分之一有效。这不是十分之一,千分之一,千分之一等等。

function round_up($number, $precision = 2)
{
    $fig = pow(10, $precision);
    return (ceil($number * $fig) / $fig);
}

Results:

结果:

var_dump(round_up(1024.654321, 0)); // Output: float(1025)
var_dump(round_up(1024.654321, 1)); // Output: float(1024.7)
var_dump(round_up(1024.654321, 2)); // Output: float(1024.66)
var_dump(round_up(1024.654321, 3)); // Output: float(1024.655)
var_dump(round_up(1024.654321, 4)); // Output: float(1024.6544)
var_dump(round_up(1024.654321, 5)); // Output: float(1024.65433)
var_dump(round_up(1024.654321, 6)); // Output: float(1024.654321)

Notes:

注:

Thanks for the contributions from Joseph McDermott and brandom for improving my original snippet.

感谢Joseph McDermott和brandom对改进我的原始代码片段的贡献。

#3


10  

Ceil will do that for you

西儿会帮你的

Taken from the example:

从示例:

<?php
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3
?>

#4


10  

I know this question has long since been answered, but it came up when I did a google search on the topic. If you want to round up with precision, then a good method would be to use the ceil function and times the number by how many decimal points you want to represent and then divide by that number.

我知道这个问题已经有很长一段时间了,但是当我在谷歌上搜索这个话题时,它出现了。如果你想要精确地四舍五入,那么一个好的方法就是使用ceil函数乘以你想要表示的小数点然后除以这个数字。

ceil(1024.321*100)/100

would produce 1024.33

会产生1024.33

#5


2  

I like Ash's response, although I would have:

我喜欢Ash的回答,尽管我可能会:

$fig = (int) str_pad('1', $precision + 1, '0');

Makes sense that if I provide precision '2', I would expect it rounded to 2 decimal places. Matter of choice though I suppose. Thanks for the answer Ash, works well.

如果我提供精确的'2',我希望它四舍五入到小数点后两位。不过我想这是一个选择的问题。谢谢你的回答,阿什,很好。

#6


2  

I like Ash's answer and Joseph's choice so I suggest to:

我喜欢艾什的回答和约瑟夫的选择,所以我建议:

$fig = pow(10, $precision);

It should work also with zero and negative precision values es:

它也应该工作为零和负精度值es:

var_dump(round_up(1024.654321, -1)); // Output: float(103)