开发者

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

开发者 https://www.devze.com 2023-02-04 20:50 出处:网络
I get开发者_开发技巧 ajava.lang.NumberFormatException: For input string: \"1.7023484830876092\"

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.


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....


It looks like a float. Especially since the first "." is the only one.


Do you perhaps want BigDecimal? It's variably sized to accommodate any number, so long as you have the memory. Use as new BigDecimal(stringNumber). Downside is you don't get access to the standard infix operators(eg. + - * / etc...).

But if you just want the largest value that can be held by a primitive then use Long.MAX_VALUE


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号