permaculture playing cards
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parseDouble question" Watch "parseDouble question" New topic

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);

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.

Keith Lynn
Ranch Hand

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

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

[ 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: 15101

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.

I agree. Here's the link:
subject: parseDouble question
It's not a secret anymore!