50

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

Community
  • 1
  • 1
LIGHT
  • 5,604
  • 10
  • 35
  • 78
  • 3
    That's not what `ceil` does: "Returns the next highest integer value by rounding up value if necessary.". You'll need a different method – Pekka Nov 23 '11 at 09:13
  • please point out why none of http://stackoverflow.com/search?q=round+to+precision+php did help solve your problem – Gordon Nov 23 '11 at 09:30
  • This is a duplicate of the following post about rounding numbers up to a number of decimal positions: http://stackoverflow.com/questions/2074527/php-rounding-numbers – Aad Mathijssen Nov 18 '14 at 14:19

2 Answers2

93

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

?>
tomexx
  • 2,301
  • 2
  • 20
  • 33
  • 2
    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 – LIGHT Nov 23 '11 at 09:20
  • You don't need a custom function - you can use the flag `PHP_ROUND_HALF_UP`, as per my answer – Adam Hopkinson Nov 23 '11 at 13:48
  • It won't work as desired. Try number 3.6333333333333... – tomexx Nov 23 '11 at 14:50
  • 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 May 03 '13 at 14:14
  • 3
    This function is not really the best. It could be much simpler. Like in this http://stackoverflow.com/questions/2074527/php-rounding-numbers/2074540#2074540 – Jan.J Jun 11 '13 at 06:23
  • 1
    @tomexx, on php 5.3 your function could produce wrong results. See sandbox [here](http://sandbox.onlinephpfunctions.com/code/e403c61a1d270ada6063bb3f59a63a2e5a6890fd) – userlond Apr 29 '15 at 03:17
  • 13
    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); }` – Nico Westerdale Nov 01 '16 at 20:29
  • 2
    `In PHP 5.6 onwards, you may prefer to use the ** operator.` (instead of `pow()`) – jave.web Feb 16 '17 at 17:35
  • See this answer https://stackoverflow.com/a/48456940/1245149 – Mojo Jan 26 '18 at 07:03
  • Tomexx, may the Holy Grand Sacrificied Mina bless you and your family for this wise answer! – Mariuta Mihai Dan Feb 15 '21 at 15:51
6

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)

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • 18
    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: http://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. – Fabio Nolasco May 03 '13 at 14:18
  • Just a note: This solution no longer answers the question since it has been edited. – helvete Nov 03 '17 at 15:11