This tutorial shows you how to convert String
to double
or int
and vice versa in Dart, which also works in Flutter.
Convert String
to double
Using parse()
For converting a String to a double, the parse()
static method of double
class can be used.
external static double parse(
String source,
[@deprecated double onError(String source)?]
);
It supports String without a decimal point in which case one digit (0) will be added after the decimal point.
double result = double.parse('2');
print(result); // 2.0
This is another example where the value already has some digits after the decimal point.
double result = double.parse('2.123');
print(result); // 2.123
Negative value is also supported.
double result = double.parse('-3.5');
print(result); // -3.5
The minimum value is double.minPositive
(5e-324), while the maximum value is double.maxFinite
(1.7976931348623157e+308). Outside that limit, it will return Infinity.
double result = double.parse('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000');
print(result); // Infinity
FormatException
will be thrown if you pass a value that cannot be parsed to double.
// below will throw FormatException: Invalid double
double result = double.parse('invalid');
If the string is null, it will throw NoSuchMethodError
error.
// below will throw NoSuchMethodError: The getter 'length' was called on null.
double result = double.parse(null);
Using tryParse()
tryParse() static method is the alternative if you want to get null value instead of exception being thrown.
external static double? tryParse(String source);
With valid values, it should work and produce the same results as using parse()
.
double result = double.parse('2');
print(result); // 2.0
double result = double.parse('2.123');
print(result); // 2.123
double result = double.parse('-3.5');
print(result); // -3.5
If the value is not within limit, the result will be Infinity.
double result = double.parse('100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000');
print(result); // Infinity
In a condition where the value is invalid, null will be returned instead of throwing an exception.
double result = double.parse('invalid');
print(result); // null
If the string is null, it will also throw NoSuchMethodError
error.
// below will throw NoSuchMethodError: The getter 'length' was called on null.
double result = double.parse(null);
Convert String
to int
Using parse()
Dart's int
has a static method parse()
that allows you to pass a String to be parsed to integer. It supports an optional radix
named parameter. If not passed, by default the radix is set to 10 (decimal number).
external static int parse(
String source,
{int? radix, @deprecated int onError(String source)?}
);
The first example is a valid positive integer.
int result = int.parse('2');
print(result); // 2
It also supports negative value of course.
int result = int.parse('3');
print(result); // 3
If you want to parse a binary String, set the radix
to 2.
int result = int.parse('100', radix: 2);
print(result); // 4
There's a limitation for the biggest (positive) number and the smallest (negative) number that can be parsed. It depends on the platform. For example, in Android and iOS, the valid values range from -2^63 (-9223372036854775809) to 2^63 - 1 (9223372036854775808) (inclusive). Passing a value outside that range will throw FormatException
.
// below will throw FormatException: Positive input exceeds the limit of integer
int result = int.parse('9223372036854775809');
It's only valid if the passed String contains number only. It doesn't support comma or E notation. You need to format the value first if the value contains non-numeric character to avoid FormatException
.
// below will throw FormatException: Invalid radix-10 number (at character 1)
int result = int.parse('3.5');
The same also applies if you pass a String without any numeric character.
// below will throw FormatException: Invalid radix-10 number (at character 1)
int result = int.parse('invalid');
The string must not be null. Otherwise it will throw an invalid argument exception.
// below will throw Invalid argument(s): The source must not be null
int result = int.parse(null);
Using tryParse()
tryParse()
works like parse()
, except it will return null if the value is invalid, instead of throwing an exception. However, the value must not be null.
external static int? tryParse(String source, {int? radix});
Passing valid values should produce the same results as using parse();
int result = int.tryParse('2');
print(result); // 2
int result = int.tryParse('3');
print(result); // 3
int result = int.tryParse('100', radix: 2);
print(result); // 4
If the given value is invalid, it will return null.
int result = int.parse('9223372036854775809')
print(result); // null
int result = int.parse('3.5');
print(result); // null
int result = int.parse('invalid');
print(result); // null
The same error will be thrown if you pass a null value.
// below will throw Invalid argument(s): The source must not be null
int result = int.parse(null);
Convert double
to String
Using toString
double
class has an instance method for conversion to String.
String toString();
Below is the example.
double d = 1.5;
String result = d.toString();
print(result); // 1.5
A decimal point along with a digit (0) after decimal point will be added if the value doesn't have a decimal point.
double d = 1;
String result = d.toString();
print(result); // 1.0
If the value is NaN (Not-a-Number), the result will be null.
double d = double.nan;
String result = d.toString();
print(result); // null
As does if the value is infinity.
double d = double.infinity;
String result = d.toString();
print(result); // null
If the passed value is null or has not been initialized, the result will be null as well.
double d
String result = d.toString();
print(result); // null
Convert double
to int
Using toString
To convert a value with int
type to String
, use instance method toString()
.
String toString();
Below is the example.
int i = 10;
String result = i.toString();
print(result); // 10
If the value is null or has not been initialized, the result will be null too.
int i;
String result = i.toString();
print(result); // null
You can also read our tutorial about how to do conversion between double
and int
.