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,705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

690 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