• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

quick php "round" off question

ckevin

New Member
How can I round off integers so that it can have exactly 1 dec. place?

E.g. if I use PHP "round" command:

when the value is 4, the outcome is 4
the value is 4.123, the outcome is 4.1,

what I really want is value 4 --> outcome 4.0
value 4.123 --> outcome 4.1

Thanks!
 
from php.net
float round ( float val [, int precision])


Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

So it looks like

round (4, 1) will return 4.0
 
bigperm, I have tried, but the outcome is "4" only...


PHP:
<? 

$row = round(4,1) ;
echo "$row" ;

?>

You can try yourself, and my php version is 4.0.6 :rolleyes:

Thanks!

Kevin
 
Round won't do what you're trying to accomplish, use printf instead (or sprintf if you're doing something with the variable other than printing it).

printf("%.1f",$row);
 
Back
Top