This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes parseDouble question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parseDouble question" Watch "parseDouble question" New topic
Author

parseDouble question

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

I could not find the answer to my question in the SEARCH feature so I thought I'd post here.

Here is my code:

String t = ("17514000.00");

double dblTest = Double.parseDouble(t);
System.out.println(dblTest);

The output comes out as = 1.7514E7

I need it come out as = 17514000.00

I guess I thought the parseDouble method would help me with this. Any suggestions or direction would be appreciated.

Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You might check out DecimalFormat
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could also try the BigDecimal class.


Garrett
[ February 01, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

You need to be aware of this: A double is just a number. It doesn't have any inherent format.

When you want to print a number to the screen, it has to be converted to text (digits, '1', '7', '5', etc.). When you convert a number to text, you do this using a specified format.

The method parseDouble in class Double does the opposite: it parses text and converts it to a number. As I said above, once the text is converted to a number, it's just a number, and it doesn't have a format.

If you use println to print a double, you don't specify the format which is to be used to convert the number back to text, so println uses a default format, and your number comes out as "1.7514E7".

If you want to explicitly specify how the number should be formatted, you should use a class like java.text.DecimalFormat. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

Hey guys,

Thanks so much for your time and all the replies. Everyone's suggestions have helped tremendously.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parseDouble question