A quick one this. How can I round down a decimal number for example: 0.305 to 0.30 with php? Using round($var) just rounds up.
| SitePoint Sponsor |


A quick one this. How can I round down a decimal number for example: 0.305 to 0.30 with php? Using round($var) just rounds up.


Read the Manual pages
PHP Code:round(0.305, 2, PHP_ROUND_HALF_DOWN);


thanks but I get an error : Warning: Wrong parameter count for round().


What version of PHP are you using? You need to be using at least 5.3 for the third parameter to be handled.




The only way I can think of (and this is a hack) is
PHP Code:$value = floor(.305 * 100) / 100; // multiply by 100 so you get 30.5, then use floor() to drop the decimal place, then divide by 100 to get .30 or .3
var_dump($value);


Cheers, will try that now. And.... worked a treat. Many thanks.
Bookmarks