Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

How to check double length?

Thread view: 
harry - 18 Mar 2006 10:43 GMT
I need to be able to validate a double value to make sure it's only 11
numbers (inc .) long, the number can aslo be negative -

   double x = -99999999.99;
   System.out.println(Double.toString(Math.abs(x)));
// produces 9.9999999999E8
   System.out.println(Double.valueOf(Double.toString(Math.abs(x))));
// produces 9.9999999999E8
   System.out.println(x);
// produces -9.9999999999E8

Is there a simple was to convert -99999999.99 to "99999999.99" to check it'e
length?

thanks

harry
harry - 18 Mar 2006 11:12 GMT
> I need to be able to validate a double value to make sure it's only 11
> numbers (inc .) long, the number can aslo be negative -
[quoted text clipped - 13 lines]
>
> harry

This seems to work -

   DecimalFormat fmtDecimal = new DecimalFormat("0.00");

   double x = -99999999.99;
   String buf = fmtDecimal.format(Math.abs(x));

   if(buf.length() <= maxLength)
       System.out.println("true");
   else
       System.out.println("false");

Is this the best/correct/most efficient way?
Ingo R. Homann - 20 Mar 2006 11:21 GMT
Hi harry,

> I need to be able to validate a double value to make sure it's only 11
> numbers (inc .) long, the number can aslo be negative -
[quoted text clipped - 13 lines]
>
> harry

What do you mean with "length" of a double?

Obviously, a double has no "length". It has a certain precision (which
does not have anything to do with the "length" of the decimal
representation of that double).

Obviously, you know the class DecimalFormat. Do you also know about
binary representation of floating point values?

Are you interested the length of a String? (Perhaps a String
representing a floating point value in decimal?)

Ciao,
Ingo
harry - 20 Mar 2006 12:30 GMT
> Hi harry,
>
[quoted text clipped - 30 lines]
> Ciao,
> Ingo

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?
Ingo R. Homann - 20 Mar 2006 15:30 GMT
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
Roedy Green - 20 Mar 2006 22:48 GMT
>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.

Roedy Green - 20 Mar 2006 22:48 GMT
>I need to be able to validate a double value to make sure it's only 11
>numbers (inc .) long, the number can aslo be negative -

another way of specifying that is that your number must lie in the
range

10000000000 .. 99999999999
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.