Long.parseLong ("string too large string") throws java.lang.NumberFormatException

I get java.lang.NumberFormatException: For input line: "1.7023484830876092"

trimming the string to 1.70234 solves the problem, but before cutting the string, I wonder if some java methods can be used to use the capacity of the target.

Regards, Jeroen.

+3
source share
4 answers

you can 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();
  }

it might work.

+3
source

It looks like a float. Moreover, the first "." is the only one.

+1
source

, BigDecimal? , . new BigDecimal(stringNumber). , infix (, + - */etc...).

But if you just want the largest value that the primitive can hold, use Long.MAX_VALUE

+1
source

You cannot parse floating point values ​​with Long.parseLong. Use Double.parseDouble instead.

+1
source

Source: https://habr.com/ru/post/1785382/


All Articles