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)

Share:
66,875

Related videos on Youtube

LIGHT
Author by

LIGHT

lazy af

Updated on July 09, 2022

Comments

  • LIGHT
    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

  • LIGHT
    LIGHT over 12 years
    No, 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
    Adam Hopkinson over 12 years
    You don't need a custom function - you can use the flag PHP_ROUND_HALF_UP, as per my answer
  • tomexx
    tomexx over 12 years
    It won't work as desired. Try number 3.6333333333333...
  • Fabio Nolasco
    Fabio Nolasco almost 11 years
    The 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
    Fabio Nolasco almost 11 years
    The 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
    Jan.J almost 11 years
    This function is not really the best. It could be much simpler. Like in this stackoverflow.com/questions/2074527/php-rounding-numbers/…
  • userlond
    userlond about 9 years
    @tomexx, on php 5.3 your function could produce wrong results. See sandbox here
  • Nico Westerdale
    Nico Westerdale over 7 years
    Do 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
    jave.web about 7 years
    In PHP 5.6 onwards, you may prefer to use the ** operator. (instead of pow())
  • helvete
    helvete over 6 years
    Just a note: This solution no longer answers the question since it has been edited.
  • Mojo
    Mojo over 6 years
  • Mariuta Mihai Dan
    Mariuta Mihai Dan about 3 years
    Tomexx, may the Holy Grand Sacrificied Mina bless you and your family for this wise answer!