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,701 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

823 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