代码之家  ›  专栏  ›  技术社区  ›  Louis Rhys

如何检查字符串是否可解析为double?[副本]

  •  64
  • Louis Rhys  · 技术社区  · 15 年前

    是否有本地方法(最好不实现自己的方法)来检查字符串是否可以用 Double.parseDouble() ?

    6 回复  |  直到 10 年前
        1
  •  51
  •   Sled bayer    10 年前

    常用的方法是用正则表达式检查它,就像在 Double.valueOf(String) 文档。

    try catch 仍然是一种选择。

    final String Digits     = "(\\p{Digit}+)";
    final String HexDigits  = "(\\p{XDigit}+)";
    // an exponent is 'e' or 'E' followed by an optionally 
    // signed decimal integer.
    final String Exp        = "[eE][+-]?"+Digits;
    final String fpRegex    =
        ("[\\x00-\\x20]*"+ // Optional leading "whitespace"
        "[+-]?(" +         // Optional sign character
        "NaN|" +           // "NaN" string
        "Infinity|" +      // "Infinity" string
    
        // A decimal floating-point string representing a finite positive
        // number without a leading sign has at most five basic pieces:
        // Digits . Digits ExponentPart FloatTypeSuffix
        // 
        // Since this method allows integer-only strings as input
        // in addition to strings of floating-point literals, the
        // two sub-patterns below are simplifications of the grammar
        // productions from the Java Language Specification, 2nd 
        // edition, section 3.10.2.
    
        // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
        "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
    
        // . Digits ExponentPart_opt FloatTypeSuffix_opt
        "(\\.("+Digits+")("+Exp+")?)|"+
    
        // Hexadecimal strings
        "((" +
        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "(\\.)?)|" +
    
        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
    
        ")[pP][+-]?" + Digits + "))" +
        "[fFdD]?))" +
        "[\\x00-\\x20]*");// Optional trailing "whitespace"
    
    if (Pattern.matches(fpRegex, myString)){
        Double.valueOf(myString); // Will not throw NumberFormatException
    } else {
        // Perform suitable alternative action
    }
    
        2
  •  63
  •   BullyWiiPlaza    6 年前

    Apache ,像往常一样,有一个很好的答案 Apache Commons-Lang 以…的形式 NumberUtils.isCreatable(String) .

    手柄 null s、 没有 try catch 需要阻止。

        3
  •  60
  •   jdc0589    15 年前

    try
    {
      Double.parseDouble(number);
    }
    catch(NumberFormatException e)
    {
      //not a double
    }
    
        4
  •  10
  •   CoolBeans Jake    15 年前

    下面的内容就足够了:-

    String decimalPattern = "([0-9]*)\\.([0-9]*)";  
    String number="20.00";  
    boolean match = Pattern.matches(decimalPattern, number);
    System.out.println(match); //if true then decimal else not  
    
        5
  •  10
  •   ruhong    9 年前

    Doubles.tryParse(String) . 你用它就像 Double.parseDouble null 而不是在字符串未解析为double时引发异常。

        6
  •  8
  •   Zach-M    13 年前

    如果希望准确地遵循Java规范,请使用以下命令:

    private static final Pattern DOUBLE_PATTERN = Pattern.compile(
        "[\\x00-\\x20]*[+-]?(NaN|Infinity|((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)" +
        "([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|" +
        "(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))" +
        "[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
    
    public static boolean isFloat(String s)
    {
        return DOUBLE_PATTERN.matcher(s).matches();
    }
    

    此代码基于 Double