marzbuzz
asked on
Converting String input to double using NumberFormat. Got error: java.lang.NumberFormatException: For input string: "154,216.48"
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()/c alculateTo talShares( );
double NAVFormmated;
NumberFormat nf = NumberFormat.getNumberInst ance();
nf.setMaximumFractionDigit s(2);
nf.setMinimumFractionDigit s(2);
String formattedNAV = nf.format(NAV);
NAVFormmated=Double.parseD ouble(form attedNAV); //error!
return NAVFormmated;
}
java.lang.NumberFormatExce ption: 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.
public double calculateNAV()
{
double NAV=calculateMarketCap()/c
double NAVFormmated;
NumberFormat nf = NumberFormat.getNumberInst
nf.setMaximumFractionDigit
nf.setMinimumFractionDigit
String formattedNAV = nf.format(NAV);
NAVFormmated=Double.parseD
return NAVFormmated;
}
java.lang.NumberFormatExce
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.
ASKER
Sounds interesting. But this is part of a homework. So I think it should be fine to lose some precision.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
> 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
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
Glad I could help! :-)
i want to convert string st="jan" to duoble
if i do Double.parseDouble(st); // i am getting Numberformat exception
if i do Double.parseDouble(st); // i am getting Numberformat exception
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