Solved

Converting String input to double using NumberFormat. Got error: java.lang.NumberFormatException: For input string: "154,216.48"

Posted on 2004-04-02
7
2,691 Views
Last Modified: 2012-06-27
Hi I've got a input string ("154,216.48")  with: "," as separator which I should convert to a double

  public double calculateNAV()
  {
    double NAV=calculateMarketCap()/calculateTotalShares();
    double NAVFormmated;
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    String formattedNAV = nf.format(NAV);
    NAVFormmated=Double.parseDouble(formattedNAV); //error!
    return NAVFormmated;
  }

java.lang.NumberFormatException: For input string: "154,216.48"
so, how can I convert this value to a double?
I know DecimalFormat with pattern should be able to fix it. But is that the best way to do it.

Indeed what I need is to convert a 12345.6789 to 12345.68. What's the best way to do it? Thanks.

0
Comment
Question by:marzbuzz
7 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 10746152
You can not reliably contain a value with 2 decimals in a double.  In the double the decimals are stored in binary format so the only decimals that van be stored precicely are those that you may get by dividing by 2.  1/2, 1/4, 1/8, 7/8.

Actually we have the same problem in the decimal system. The only presice fractions we can write are those that come from dividing by 2 or 5. any other fraction will require rounding to limit the number of decimals. 1/3 becomes 0.333333333333..., etc.

You desire for 2 digits suggest you are working with money. the easiest solution is to store them in a long (as cents) that way all calculations will be correct. Later when you write the number for people to read you write them as dollars (with 2 decimals) again.

regards JakobA
0
 

Author Comment

by:marzbuzz
ID: 10746268
Sounds interesting. But this is part of a homework. So I think it should be fine to lose some precision.
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 30 total points
ID: 10746310
I think you've misunderstood the use and purpose of NumberFormat, however it is a creative way of the class cluster.

DecimalFormat nf = new DecimalFormat("#########.00");  // supports numbers up to 999,999,999.99
String formattedNAV = nf.format(NAV);
NAVFormmated=Double.parseDouble(formattedNAV); // no error!
return NAVFormmated;
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:marzbuzz
ID: 10746554
Thanks orangehead911. I guess you are right. Using NumberFormat is a wrong choice. Just not that familiar with DecimalFormat. I guess DecimalFormat class is the best way to deal with this kind of question.
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10748159
>  In the double the decimals are stored in binary format.
It's much more complicated than that.
Doubles in java follow the IEEE 754 standard (composed of sign, exponent and mantissa):
http://www.trotek.ec-lyon.fr/~muller/cours/numeration/flp_ieee.html.en
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10750850
Glad I could help! :-)
0
 

Expert Comment

by:vinod_063
ID: 12704629
i want to convert string st="jan" to duoble

if i do Double.parseDouble(st);  // i am getting Numberformat exception
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now