Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
2,726 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 90 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
Industry Leaders: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

636 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