Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding up to the second decimal place [duplicate]

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

like image 279
LIGHT Avatar asked Nov 23 '11 09:11

LIGHT


People also ask

What is the number 2.738 correct to 2 decimal places?

What is 2.738 Round to Two Decimal Places? In the given number 2.738, the digit at the thousandths place is 8, so we will add 1 to the hundredths place digit. So, 3+1=4. Therefore, the value of 2.738 round to two decimal places is 2.74.

How do you print double up to 2 decimal places?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

How do you round to 2 decimal places in Excel?

Head to Formulas > Math & Trig, and then choose either the “ROUNDUP” or “ROUNDDOWN” function from the dropdown menu. Enter the number (or cell) you want to round in the “Number” field. Enter the number of digits to which you want to round in the “Num_digits” field.


2 Answers

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  ?> 
like image 125
tomexx Avatar answered Sep 30 '22 19:09

tomexx


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)

like image 20
Adam Hopkinson Avatar answered Sep 30 '22 18:09

Adam Hopkinson