Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

parseDouble question

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might check out DecimalFormat
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try the BigDecimal class.


Garrett
[ February 01, 2006: Message edited by: Garrett Rowe ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15751
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

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

Regards.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!