# reducing decimals

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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 ?

Albert

## Re: reducing decimals

.oO(albert)

Have a look at round()'s second parameter ...

Micha

## Re: reducing decimals

Michael Fesser wrote:

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

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

albert wrote:

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

Albert:

Try with the BCDIV() function, with 3 parameters: \$number_1,\$number_2
and \$number_of_decimal