number_format() question

liunx

Guest
i'm using number format to only show one digit after a decimal, so it's like this:

$floater = 9.98;
$floater = number_format($floater,1);

so now $floater = 9.9.

the problem arises when $floater = 9
I want it to NOT add a decimal and a 0, however number_format(9,1) yields 9.0. Is there another way to format numbers so it will only adjust the numbers if they require it?you can use

round(), but that will round to the nearest decimal.

you might hav eto write a function to do what you want.thanks.I do that alot in Perl using the split function. Maybe PHP has something similar.

You split the integer at the decimal into two strings

$yournumber = 9.00;
($integer, $decimal) = split(/\./, $yournumber);
if ($decimal == 0 || $decimal == "") {
$yournumber = $integer;
}
else {
$yournumber = $integer . '.' . $decimal;
}

something like that anyway. You could also do it using regexp.
 
Back
Top