php 四舍五入到小数点后第二位

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

Rounding up to the second decimal place

phpnumbersroundingceil

提问by LIGHT

Possible Duplicate:
PHP Round function - round up to 2 dp?

可能的重复:
PHP Round 函数 - 舍入到 2 dp?

What my problem is:

我的问题是:

When i use

当我使用

ceil(3.6451895227869);

i get like

我喜欢

4

but i want

但我想要

3.65

Can you help me out?

你能帮我吗?

UPDATE

Please remember: This should always round to ceil like while rounding

请记住:这应该总是像四舍五入一样舍入到天花板

3.6333333333333

3.6333333333333

it must not be 3.63but should be 3.64

它不能是3.63而应该是3.64

回答by tomexx

Check out http://www.php.net/manual/en/function.round.php

查看http://www.php.net/manual/en/function.round.php

<?php

echo round(3.6451895227869, 2);

?>

EDITTry using this custom function http://www.php.net/manual/en/function.round.php#102641

编辑尝试使用这个自定义函数http://www.php.net/manual/en/function.round.php#102641

<?php 
function round_up ( $value, $precision ) { 
    $pow = pow ( 10, $precision ); 
    return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow; 
} 

echo round_up(3.63333333333, 2);  // 3.64

?>

回答by Adam Hopkinson

You want round

你要圆

round(3.6451895227869, 2, PHP_ROUND_HALF_UP);

The second argument is the precision, the flag tells roundto always round up (like ceil)

第二个参数是精度,标志告诉round总是向上取整(如ceil