How to check numeric strings in Flutter and Dart

Last updated on November 11, 2020 A Goodman Loading... Post a comment

A numeric string is just a number in string format.

Examples of numeric strings:

'123',
'0.123',
'4.234,345',
'-33.33',
'+44.44'

To check whether a string is a numeric string, you can use the double.tryParse() method. If the return equals null then the input is not a numeric string, otherwise, it is.

if(double.tryParse(String input) == null){
   print('The input is not a numeric string');
} else {
   print('Yes, it is a numeric string');
}

Example

The code:

void main(){
  if(double.tryParse('-33.230393399') == null){
    print('False');
  } else {
    print('True');
  }
}

Output:

True

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x