Connecting Tech Pros Worldwide Forums | Help | Site Map

Format float

WJ Zeeuwen
Guest
 
Posts: n/a
#1: May 14 '06
Hi all,

Is it possible to adjust the number of digits in a float?

For example $test = 1/3; echo ($test); gives 0.33333333.. But i only
want 0.33
Is there a function or do i have to use round($test*100)/100

Thanx,
WJ

Rik
Guest
 
Posts: n/a
#2: May 14 '06

re: Format float


WJ Zeeuwen wrote:[color=blue]
> Hi all,
>
> Is it possible to adjust the number of digits in a float?
>
> For example $test = 1/3; echo ($test); gives 0.33333333.. But i only
> want 0.33
> Is there a function or do i have to use round($test*100)/100[/color]

I'd use round($test,2);
Also possible is number_format();

Grtz,
--
Rik Wasmus


Jerry Stuckle
Guest
 
Posts: n/a
#3: May 14 '06

re: Format float


WJ Zeeuwen wrote:[color=blue]
> Hi all,
>
> Is it possible to adjust the number of digits in a float?
>
> For example $test = 1/3; echo ($test); gives 0.33333333.. But i only
> want 0.33
> Is there a function or do i have to use round($test*100)/100
>
> Thanx,
> WJ[/color]

If all you want to do is display the value 0.33 without actually changing the
number (which will affect future math operations), you can use

printf("%01.2f", 0.33);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Closed Thread