# How to parse a String into a number in Dart/Flutter

Converting String into Number is necessary for almost language. Dart is no exception. In this tutorial, we’re gonna look at ways to parse a String into a number (`int` & `double`) using `parse()` and `tryParse()` method. You also know how to catch Exception for invalid input string, or convert a Hex string to int in Dart/Flutter.

## Parse a String into an int in Dart/Flutter

### Using parse() method

To parse a string into an int, we use int class `parse()` method:

``````var n = int.parse('42');
// 42
``````

The input String could be signed (`-` or `+`):

``````var n1 = int.parse('-42');
// -42

var n2 = int.parse('+42');
// 42
``````

### Radix – Hex string to int

Sometimes we have to work with string in radix number format. Dart int `parse()` method also supports convert string into a number with radix in the range 2..36:

For example, we convert a Hex string into int:

``````var n_16 = int.parse('FF', radix: 16);
// 255
``````

### parse() method and catch Exception

If input string isn’t in a valid integer form, the program throws a `FormatException`:

``````var n = int.parse('n42');
/*
Exception has occurred.
FormatException (FormatException: Invalid radix-10 number (at character 1)
n42
^
*/
``````

So to handle this case, we can use Dart try-catch block:

``````try {
var n = int.parse('n42');
print(n);
} on FormatException {
print('Format error!');
}
// Format error!
``````

int class `parse()` method also gives us a way to handle `FormatException` case with `onError` parameter.

``````var num4 = int.parse("n42", onError: (source) => -1);
// -1
``````

When the exception is thrown, `onError` will be called with `source` as input string. Now we can return an integer value or null… In the example above, the function returns `-1` whenever `source` is in wrong integer literal.

### Using tryParse() method

Instead of `int.parse(string, onError: (string) => ...)`, we should use `int.tryParse(string)`:

``````var n0 = int.tryParse('42');
// 42

var n1 = int.tryParse('-42');
// -42

var n2 = int.tryParse('FF', radix: 16);
// 255
``````

### What is the different between `parse()` and `tryParse()`?

• `parse()` throws a `FormatException` for invalid input string
• `tryParse()` returns `null` for invalid input string

### tryParse() method and Exception

For `tryParse()` method, we don’t have to catch Exception, we only need to check `null` value.
The statement will be simpler:

``````var n = int.tryParse('n42') ?? -1;
// -1
``````

We can also return a string value while `parse()` method with `onError` parameter only returns integer:

``````var n = int.tryParse('n42') ?? 'Format error!';
// 'Format error!'
``````

## Parse a String into a double in Dart/Flutter

### Using parse() method

Like integer, string in double literal can be parsed into double using `parse()` method:

``````var n1 = double.parse('4.2');
// 4.2

var n2 = double.parse('0.');
// 0.0

var n3 = double.parse('.0');
// 0.0

var n4 = double.parse('-1.e3');
// -1000.0

var n5 = double.parse('123E+3');
// 123000.0

var n6 = double.parse('+.12e-2');
// 0.0012

var n7 = double.parse('+.12e-6');
// 1.2e-7

var n8 = double.parse('-NaN');
// NaN
``````

### parse() method and catch Exception

If input string isn’t in a valid double form, the program throws a `FormatException`:

``````
var n = double.parse('n4.2');
/*
Exception has occurred.
FormatException (FormatException: Invalid double n4.2)
*/
``````

We can use Dart try-catch block to handle this Exception:

``````try {
var n = double.parse('n4.2');
print(n);
} on FormatException {
print('Format error!');
}
// Format error!
``````

Dart double class `parse()` method can handle `FormatException` case with additional `onError` parameter.

``````var n = double.parse('n4.2', (source) => -1);
// -1.0
``````

### Using tryParse() method

Instead of throwing `FormatException` for invalid input string, double class `tryParse()` method returns `null`:

``````double n = double.tryParse('n4.2');
// null
``````

Using `tryParse()` method, we can check `null` then return specific value.

``````double n = double.tryParse('n4.2') ?? -1;
// -1.0
``````

## Conclusion

We’ve known how to parse a String into a number (`int` or `double`) in Dart/Flutter by using `parse()` or `tryParse()` method.

Remember that `onError` parameter is deprecated and will be removed in the future. So `tryParse()` method could be the better choice.

You can also find many Dart helpful String methods in the post:
Dart/Flutter String Methods & Operators tutorial with examples

Happy learning! See you again!

## 11 thoughts to “How to parse a String into a number in Dart/Flutter”

1. Faz says:

Your Dart tutorials has become very useful for me and yes it offers great written content.

2. ube says:

Hey there and thank you for the tutorial! Keep working 🙂

3. harry says:

Great goods from you, man! I really like all tutorials right here.
I can not wait to learn far more from you. This is actually a great site.

4. Chef says:

Thanks!

5. alfredo says:

Superb, what a tutorial! Keep it up.

6. Mass says:

Comprehensive tutorial. Thanks!

7. Davolt says:

I love reading this tutorial. Thank a lot!

8. Djoudi says:

Hi,

Thank you for this amazing website, keep up the good work.

Regards

9. gonzales says:

This Dart tutorial is extremely informative, it shows the simple way to parse a String to a number.

10. jizzyou says:

I’ve been wanting to take up mobile app development with flutter so I thought I’d get comfortable with dart first before diving into flutter. So far, I’m happy. Coming from Javascript, Dart is easy to pick up, and fun to learn.

11. koraline says: