Hi,
> Yes sorry, didn't explain it too well did I!
>
> I wanted to convert a double to a string in order to check how many digits
> were in it - in my followup post I found a way of doing this which I hope is
> the correct way?
I read your followup post by I guess you did not get my point.
Example: The decimal numer 0.1 cannot be represented by a double,
because a double only has a limited precision. (That's similar to the
fact that 1/3 cannot be represented by a decimal coded floating point
number.) Now, if you want to "fill" the value 0.1 into a double, the
double will in fact have the value 0.0999999 or 0.1000001 or something
like that (that is similar to 1/3 which can only be approximately coded
by 0.33333333). (*)
So, if you count the number of decimals in the *String* "0.1", that is
something totaly different to counting something within the double. It
just does not make any sense to count something within the double. It is
similar to asking "How many decimals has the fraction 1/3" It just
depends on when you stop counting! It could be 5, if you represent it as
"0.33333" or it could be 9 if you represent it as "0.333333333".
Ciao,
Ingo
(*) Note that System.out.println(0.1) tries to hide that loss of
precision so that it is hard to reproduce that. Try playing with
somehting like that:
double d=0.001;
double sum=0;
for(int i=0;i<1000;i++) {
sum+=d;
}
System.out.println(sum);
Patricia Shanahan - 20 Mar 2006 15:44 GMT
> Hi,
>
[quoted text clipped - 22 lines]
> depends on when you stop counting! It could be 5, if you represent it as
> "0.33333" or it could be 9 if you represent it as "0.333333333".
I think the OP means something like "the number of characters in the
decimal approximation produced by this format, ignoring any minus sign".
However, in conversion from binary floating point to decimal there is no
equivalent of the problem of the decimal representation of 1/3. EVERY
number that can be expressed in a finite number of bits of binary
floating point can also be exactly represented in a finite number of
decimal digits. In that sense, one could meaningfully ask for the number
of digits in the shortest exact decimal representation of any double. It
just isn't what the OP wanted to know.
Patricia
Ingo R. Homann - 20 Mar 2006 16:00 GMT
Hi Patricia,
> I think the OP means something like "the number of characters in the
> decimal approximation produced by this format, ignoring any minus sign".
[quoted text clipped - 6 lines]
> of digits in the shortest exact decimal representation of any double. It
> just isn't what the OP wanted to know.
I agree to you, but I just do not know what harry wants to know. I think
- and that was what I wanted to express - that what he wants (or: "what
I think he wants") does not make sense.
Ciao,
Ingo
>I wanted to convert a double to a string in order to check how many digits
>were in it - in my followup post I found a way of doing this which I hope is
>the correct way?
to do that see http://mindprod.com/applets/converter.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.