Jump to content

PHP number rounding


johnsmith153

Recommended Posts

I need a formula for rounding a number to 2 decimal places, but the last number must be a 5 or 0 (i.e. 1 dp if it is 0)

 

 

so:

 

1.4798 = 1.5

5.4567 = 5.45

1.2356 = 1.25

1.2011 = 1.2

1.1945 = 1.2

 

 

function roundTo($number, $to){
    return number_format(round($number/$to, 0)* $to, 2);
}

echo roundTo($number,.05);

 

taken from

http://www.php.net/manual/en/function.round.php#93747

 

Link to comment
https://forums.phpfreaks.com/topic/181423-php-number-rounding/#findComment-957058
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.