PDA

View Full Version : PHP floor to closest value.



muneeba9071
10-09-2011, 07:23 PM
Hi Guys,

I need your help. How to floor value to closest 10 like this:



3.64 should return 3.60
3.69 should return 3.70
3.99 should return 4.00


Any ideas?

Many Thanks

Fou-Lu
10-09-2011, 08:04 PM
Don't bother with recalculating them, just use a print formatter:


$a = 3.64;
$b = 3.69;
$c = 3.99;

printf('$a = %0.1f, $b = %0.1f, $c = %0.1f', $a, $b, $c);


Alternatively, use round() or number_format() to do what you want.

muneeba9071
10-09-2011, 08:07 PM
Thanks Fou-Lu.

Is it possible to make it return with the extra zero, like this:



3.60
3.70
4.00


I have tried number_format($a,2), but it return the same thing.

Thanks

Fou-Lu
10-09-2011, 08:20 PM
Rewrap the results:


$a = 3.64;

$b = round($a, 1);
print number_format($b, 2);