Search code examples
javalong-integerstring-parsing

Long.parseLong("digitstring too significant") produces java.lang.NumberFormatException


I get a java.lang.NumberFormatException: For input string: "1.7023484830876092"

trimming the string to 1.70234, does solve the problem, but before slicing the string myself I'm wondering whether its possible to use some java methods to leverage the capacity of the target object.

kind regards, jeroen.


Solution

  • you could try using the DecimalFormat class:

    http://download.oracle.com/javase/1.5.0/docs/api/java/text/DecimalFormat.html

      DecimalFormat fm = new DecimalFormat("#.################");
      try {
       double x = fm.parse("1.12345678901234").doubleValue();
       System.out.println(x);
      } catch (ParseException e) {
       e.printStackTrace();
      }
    

    this might work....