I'm using jsp for storing dvds into a database
For price I chose to use float. In the form I use the next approach:
<form method="POST">
...
<td>
<input type="text" name="dvdprice"/>
</td>
...
<input type="submit" name="submit" value="Submit">
</form>
When trying to convert it from string to float I use this
String price = request.getParameter("dvdprice");
...
Float.valueOf(price)
And I get this exception:
SEVERE: java.lang.NumberFormatException: For input string: "9.99"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.valueOf(Integer.java:582)
...
I've searched for this problem all over the place but I couldn't find a solution.. I used this technique to convert the numbers millions of times and I'm not sure why isn't working right now...
Any help?
The exception you're getting isn't from a call to Float.parseFloat
. It's from a call to Integer.parseInt
. Check the line number in the stack trace to find out where. I suspect you're parsing integers elsewhere, and you're just pulling the wrong value to parse.
Additionally, strongly consider using BigDecimal
instead of either float
or double
to store currency values. (Binary floating point types don't play nicely with quantities which have a precise decimal representation.)