Web Hosting Talk







View Full Version : PHP Rounding


Goldfiles
06-02-2007, 05:57 PM
I want to round to exactly 2 decimal places, even if the number starts out with no decimals.

Example:
$number = 2;
I want this to round to 2.00

I am using this code right now, but I dont know how to tell it to always round to 2 decimal places even if it means tacking on two zeros at the end.

$number = round($number, 2);

Should be an easy fix for this. Help?

brendandonhu
06-02-2007, 06:42 PM
I think you might be able to get that with money_format() or str_pad().

WHMCS-Matt
06-02-2007, 07:26 PM
You should use the sprintf function to do this:


$number = sprintf("%01.2f", $number);

Matt

Chad Phillips
06-02-2007, 10:29 PM
http://ca3.php.net/number_format


number_format((float) $number, 2);