Rounding up to the second decimal place [duplicate]

只谈情不闲聊 提交于 2019-12-18 11:00:06

问题


Possible Duplicate:
PHP Round function - round up to 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

it must not be 3.63 but should be 3.64


回答1:


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

<?php

echo round(3.6451895227869, 2);

?>

EDIT Try using this custom function 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

?>



回答2:


You want round

round(3.6451895227869, 2, PHP_ROUND_HALF_UP);

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



来源:https://stackoverflow.com/questions/8239600/rounding-up-to-the-second-decimal-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!