Rounding up to the second decimal place
66,875
Solution 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
?>
Solution 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
)
Related videos on Youtube
Comments
-
LIGHT almost 2 years
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
-
Pekka over 12 yearsThat's not what
ceil
does: "Returns the next highest integer value by rounding up value if necessary.". You'll need a different method -
Gordon over 12 yearsplease point out why none of stackoverflow.com/search?q=round+to+precision+php did help solve your problem
-
Aad Mathijssen over 9 yearsThis is a duplicate of the following post about rounding numbers up to a number of decimal positions: stackoverflow.com/questions/2074527/php-rounding-numbers
-
-
LIGHT over 12 yearsNo, but i want it ceil. This gonna round it off.. ok, lets see for example: i want to ceil this number: 3.63333333333 to 3.64
-
Adam Hopkinson over 12 yearsYou don't need a custom function - you can use the flag
PHP_ROUND_HALF_UP
, as per my answer -
tomexx over 12 yearsIt won't work as desired. Try number 3.6333333333333...
-
Fabio Nolasco almost 11 yearsThe PHP round + PHP_ROUND_HALF_UP will only round up if the last digit is 5 or more. Example: 3.444 will become 3.44. So the function sent by tomexx is actually really cool and works perfectly. I just tested it here.
-
Fabio Nolasco almost 11 yearsThe PHP round + PHP_ROUND_HALF_UP will only round up if the last digit is 5 or more. Example: 3.444 becomes 3.44. 3.445 becomes 3.45 Source: php.net/manual/en/function.round.php So the function sent by tomexx is actually really cool and works perfectly. I just tested it here. <?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 ?>
-
Jan.J almost 11 yearsThis function is not really the best. It could be much simpler. Like in this stackoverflow.com/questions/2074527/php-rounding-numbers/…
-
userlond about 9 years@tomexx, on php 5.3 your function could produce wrong results. See sandbox here
-
Nico Westerdale over 7 yearsDo not do multiplication inside a ceil function! You'll get floating point errors and it can be extremely unpredictable! Try round_up(2.22, 2) and you'll see it gives an answer of 2.23 due to floating point errors, whereas round_up(2.21, 2) correctly gives 2.21. To avoid this do:
function ceiling($value, $precision = 0) { $offset = 0.5; if ($precision !== 0) $offset /= pow(10, $precision); return round($value + $offset, $precision, PHP_ROUND_HALF_DOWN); }
-
jave.web about 7 years
In PHP 5.6 onwards, you may prefer to use the ** operator.
(instead ofpow()
) -
helvete over 6 yearsJust a note: This solution no longer answers the question since it has been edited.
-
Mojo over 6 yearsSee this answer stackoverflow.com/a/48456940/1245149
-
Mariuta Mihai Dan about 3 yearsTomexx, may the Holy Grand Sacrificied Mina bless you and your family for this wise answer!