如何确保始终使用 PHP 对浮点数进行四舍五入?

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

How can I make sure a float will always be rounded up with PHP?

phpfloating-pointroundingprecisionceil

提问by Sotiris

I want to make sure a float in PHP is rounded up if anydecimal 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?

我知道该round()函数存在,但如果找到任何小数,我看不到任何四舍五入的函数。有什么简单的方法可以做到这一点吗?

回答by ircmaxell

Use the ceilfunction:

使用ceil函数:

$number = ceil(1.1); //2

回答by ash

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

我知道这是一个古老的话题,但它出现在 Google 中。我将扩展 Blake Plumb 关于精度的回答。

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 为改进我的原始片段所做的贡献。

回答by lethalMango

Ceilwill do that for you

Ceil会为你做到这一点

Taken from the example:

取自示例:

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

回答by Blake Plumb

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

回答by Joseph McDermott

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”,我希望它四舍五入到小数点后两位,这是有道理的。虽然我想是选择问题。感谢您的回答 Ash,效果很好。