• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

parseDouble question

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might check out DecimalFormat
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also try the BigDecimal class.


Garrett
[ February 01, 2006: Message edited by: Garrett Rowe ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

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

Regards.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic