Search code examples
javaintnumberformatexception

How to prevent a number format exception caused by converting string to an int


So I've been trying to convert my string into an integer, and I'm getting a number format exception. I'm passing a string of 813.0 to the object below but it is failing.

totals.setTotalPremium(Integer.parseInt(r.getTotalpremium().replace(",","").trim()));

Solution

  • Try Double.parseDouble("813.0") as your output returns decimal values. Integer.parseInt() is helpful only in case of non decimal numbers.