Checking if string is numeric in dart

后端 未结 4 1117
悲哀的现实
悲哀的现实 2021-02-02 06:59

I need to find out if a string is numeric in dart. It needs to return true on any valid number type in dart. So far, my solution is



        
相关标签:
4条回答
  • 2021-02-02 07:13

    Even shorter. Despite the fact it will works with double as well, using num is more accurately.

    isNumeric(string) => num.tryParse(string) != null;
    

    num.tryParse inside:

    static num tryParse(String input) {
      String source = input.trim();
      return int.tryParse(source) ?? double.tryParse(source);
    }
    
    0 讨论(0)
  • 2021-02-02 07:32

    for anyone wanting a non native way using regex

    RegExp _numeric = RegExp(r'^-?[0-9]+$');
    
    /// check if the string contains only numbers
     bool isNumeric(String str) {
    return _numeric.hasMatch(str);
    }
    
    0 讨论(0)
  • 2021-02-02 07:35

    In Dart 2 this method is deprecated

    int.parse(s, onError: (e) => null)

    instead, use

     bool _isNumeric(String str) {
        if(str == null) {
          return false;
        }
        return double.tryParse(str) != null;
      }
    
    0 讨论(0)
  • 2021-02-02 07:36

    This can be simpliefied a bit

    void main(args) {
      print(isNumeric(null));
      print(isNumeric(''));
      print(isNumeric('x'));
      print(isNumeric('123x'));
      print(isNumeric('123'));
      print(isNumeric('+123'));
      print(isNumeric('123.456'));
      print(isNumeric('1,234.567'));
      print(isNumeric('1.234,567'));
      print(isNumeric('-123'));
      print(isNumeric('INFINITY'));
      print(isNumeric(double.INFINITY.toString())); // 'Infinity'
      print(isNumeric(double.NAN.toString()));
      print(isNumeric('0x123'));
    }
    
    bool isNumeric(String s) {
      if(s == null) {
        return false;
      }
      return double.parse(s, (e) => null) != null;
    }
    
    false   // null  
    false   // ''  
    false   // 'x'  
    false   // '123x'  
    true    // '123'  
    true    // '+123'
    true    // '123.456'  
    false   // '1,234.567'  
    false   // '1.234,567' (would be a valid number in Austria/Germany/...)
    true    // '-123'  
    false   // 'INFINITY'  
    true    // double.INFINITY.toString()
    true    // double.NAN.toString()
    false   // '0x123'
    

    from double.parse DartDoc

       * Examples of accepted strings:
       *
       *     "3.14"
       *     "  3.14 \xA0"
       *     "0."
       *     ".0"
       *     "-1.e3"
       *     "1234E+7"
       *     "+.12e-9"
       *     "-NaN"
    

    This version accepts also hexadecimal numbers

    bool isNumeric(String s) {
      if(s == null) {
        return false;
      }
    
      // TODO according to DartDoc num.parse() includes both (double.parse and int.parse)
      return double.parse(s, (e) => null) != null || 
          int.parse(s, onError: (e) => null) != null;
    }
    
    print(int.parse('0xab'));
    

    true

    UPDATE

    Since {onError(String source)} is deprecated now you can just use tryParse:

    bool isNumeric(String s) {
     if (s == null) {
       return false;
     }
     return double.tryParse(s) != null;
    }
    
    0 讨论(0)
提交回复
热议问题