Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Format Exception

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

Trying to take a value "subTotal" and have a running total on it. Once running total is complete, I need to do a number format on it and display. Everthing compiles fine. But when I run the code, the following error occurs at:

subTotal = subTotal + new Double(FTinfo1.getDefaultValue()).doubleValue();


java.lang.NumberFormatException: $123,456.12
at java.lang.FloatingDecimal.readjavaFormatString(FloatingDecimal.java:1173)
at java.lang.Double.valueOf(Double.java:165)
at java.lang.Double.<init>(Double.java:165)
...
--if you need more info, I'll get it to you.

Code:
...
for(int k = 0; tmpRows > k; k++) {
   FieldTemplate FTinfo1 = (FieldTemplate)dataFT.elementAt(k);
   if(FTinfo1.getFieldDisplayText().equals(&quot;Amount&quot;)) {
      System.out.println(&quot;HERE 1&quot;);
      subTotal = subTotal + new Double(FTinfo1.getDefaultValue()).doubleValue();
      System.out.println(&quot;subTotal &quot; + subTotal);
      double itemTotal = subTotal;
      System.out.println(&quot;HERE 2 &quot; + itemTotal);
      NumberFormat nf = NumberFormat.getCurrencyInstance();
      System.out.println(&quot;HERE 3&quot;);
      tmpTotal = nf.format(itemTotal);
      System.out.println(&quot;HERE 4&quot;);
   }
%>
...

Any suggestions/comments would be most welcome.

TIA,
Tim
 
Looks like the value you're taking from the input field (which is a string) has a '$' character at the beginning. You'll need to do some additional formatting to remove this before passing the string into the Double constructor. I think the comma might also cause problems, so you might need to strip that out too. In other words, you need to make sure the string you pass into the constructor is formatted like &quot;123456.12&quot; rather than &quot;$123,456.12&quot;.

You can also use
Code:
Double.parseDouble(java.lang.String)
(which would do exactly the same thing as
Code:
new Double(java.lang.String).doubleValue()
)
 
Thanks for the reply wiser. I'll look into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top