程序在解析Double值时挂起?

时间:2012-02-27 04:40:17

标签: java

  

可能重复:
  Compilation hangs for a class with field double d = 2.2250738585072012e-308

为什么在解析双值0.0222507385850720119e-00306时,这个Java程序会挂起?

public class DoubleTest {

  public static boolean isDoubleValue(String value) {
    try {
      Double.valueOf(value);
      return true;
    } catch (NumberFormatException e) {
      return false;
    }
  }


  public static void main(String a[]){

    DoubleTest.isDoubleValue("0.0222507385850720119e-306");
    System.out.println("out of hang...");
  }

}

1 个答案:

答案 0 :(得分:0)

它不适合我。以下程序在Eclipse Indigo(3.7.1)中运行良好:

class Test {
    public static double isDoubleValue(String value) {
        try {
            double d = Double.valueOf(value);
            return d;
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public static void main(String a[]){
        double d = Test.isDoubleValue("0.0222507385850720119e-306");
        System.out.println("out of hang... " + d);
    }
}

输出结果为:

out of hang... 2.2250738585072014E-308

正如所料。

我将提到-308指数正好位于允许的双值的边缘,因此它可能是您特定Java版本中的错误。但是,如上所述,它并不存在于Indigo中。

相关问题