reducing decimals
am 18.08.2007 17:24:49 von Albert
Hello,
Doing a simple division, I obtain this number :
0.0024096385542168672344587321276776492595672607421875
I'd like to have 0.0024 instead.
I know the number_format function but is it the right solution ?
I thought of another solution :
$MyDec = 0.0024096385542168672344587321276776492595672607421875;
$MyDecOk = round($MyDec * 10000) / 10000 ; # => 0.0024
Is there a special function to do so ?
Thanks in advance,
Albert
Re: reducing decimals
am 18.08.2007 17:32:40 von Michael Fesser
..oO(albert)
>Doing a simple division, I obtain this number :
>0.0024096385542168672344587321276776492595672607421875
>I'd like to have 0.0024 instead.
>I know the number_format function but is it the right solution ?
>I thought of another solution :
>
>$MyDec = 0.0024096385542168672344587321276776492595672607421875;
>$MyDecOk = round($MyDec * 10000) / 10000 ; # => 0.0024
>
>Is there a special function to do so ?
Have a look at round()'s second parameter ...
Micha
Re: reducing decimals
am 18.08.2007 18:32:32 von ivansanchez-alg
Michael Fesser wrote:
>>Is there a special function to do so ?
>
> Have a look at round()'s second parameter ...
format_number() and sprintf() are also useful, if you just want to hide the
extra decimals when echo()ing the numbers.
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
Orilla: período de tiempo de 60 minutillos.
Re: reducing decimals
am 20.08.2007 11:03:59 von Albert
> Have a look at round()'s second parameter ...
I tried this :
$MyDec = 0.0024096385542168672344587321276776492595672607421875;
$MyDecOk = round($MyDec, 4) ;
The trouble is that this data is serialized in a text file afterward.
Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
I used the number_format function instead.
albert
Re: reducing decimals
am 20.08.2007 14:18:41 von Jerry Stuckle
albert wrote:
>> Have a look at round()'s second parameter ...
>
> I tried this :
>
> $MyDec = 0.0024096385542168672344587321276776492595672607421875;
> $MyDecOk = round($MyDec, 4) ;
>
> The trouble is that this data is serialized in a text file afterward.
> Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
> I used the number_format function instead.
>
> albert
>
>
>
Yep, that's what happens with floating point numbers. Floating point is
stored in base 2, not base 10. 0.0024 in this format is a repeating
number (similar to 1/3 in decimal) and cannot be represented directly.
But now you're saying that you're storing the number in a text file -
which you didn't before. That's new information. Albert's comment is
right on with the original information you gave. With the new
information, number_format should work fine.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: reducing decimals
am 21.08.2007 15:23:06 von fssm2666
On Aug 20, 8:18 am, Jerry Stuckle wrote:
> albert wrote:
> >> Have a look at round()'s second parameter ...
>
> > I tried this :
>
> > $MyDec = 0.0024096385542168672344587321276776492595672607421875;
> > $MyDecOk = round($MyDec, 4) ;
>
> > The trouble is that this data is serialized in a text file afterward.
> > Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
> > I used the number_format function instead.
>
> > albert
>
> Yep, that's what happens with floating point numbers. Floating point is
> stored in base 2, not base 10. 0.0024 in this format is a repeating
> number (similar to 1/3 in decimal) and cannot be represented directly.
>
> But now you're saying that you're storing the number in a text file -
> which you didn't before. That's new information. Albert's comment is
> right on with the original information you gave. With the new
> information, number_format should work fine.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -
>
> - Show quoted text -
Albert:
Try with the BCDIV() function, with 3 parameters: $number_1,$number_2
and $number_of_decimal
More information at....
http://cl.php.net/manual/en/ref.bc.php
http://cl.php.net/manual/en/function.bcdiv.php
Felipe Silva.