Converting strings to numbers means changing text that looks like a number—like “123” or “3.14”—into actual number values that your program can use for math or calculations. This is useful when you get input as text, for example, from users typing in a form or reading data from files. To work with these inputs as numbers, you need to convert them from strings to number types like integers or doubles.
Using int.parse()
to Convert String to Integer
To convert a string into an integer in Dart, you use the int.parse()
method. This method reads the text inside the string and turns it into a real number without decimals.
Here is a simple example:
void main() {
String strNumber = '42';
int number = int.parse(strNumber);
print('Number is $number');
}
In this code, the string '42'
is converted to the number 42
using int.parse()
. After converting, it prints out the number. This method is useful when you get numbers as text, like user input or reading from a file, and you want to work with them as numbers in your program.
You can also convert strings with spaces by using trim()
first, like this:
void main() {
String strNumber = ' 100 ';
int number = int.parse(strNumber.trim());
print('Trimmed number is $number');
}
Here, the spaces around ' 100 '
are removed before converting to the number 100
.
This way, int.parse()
helps you easily change strings into integers to use in calculations or logic.
Using double.parse()
to Convert String to Double
To convert a string into a number with decimals (a double) in Dart, you use the double.parse()
method. This method reads the string and changes it into a floating-point number.
Here’s a simple example:
void main() {
String strPi = '3.14';
double pi = double.parse(strPi);
print('Pi is $pi');
}
In this code, the string '3.14'
becomes the number 3.14
after using double.parse()
. This is useful when you get decimal numbers as text and want to do math or calculations with them.
You can also handle strings with spaces by trimming them first:
void main() {
String strNum = ' 2.718 ';
double number = double.parse(strNum.trim());
print('Number is $number');
}
Here, the spaces around ' 2.718 '
are removed before converting, so the program correctly reads the number 2.718
.
Using double.parse()
lets you easily work with decimal numbers stored as strings in your Dart programs.
Using num.parse()
for Either Integer or Double
Dart provides num.parse()
to convert a string into either an integer or a double, depending on the value. This is useful when you don’t know if the string holds a whole number or a decimal.
Here’s an example that converts both an integer string and a decimal string using num.parse()
:
void main() {
num a = num.parse('25');
num b = num.parse('3.5');
print('a is $a');
print('b is $b');
}
In this example, '25'
is converted to the integer 25
, and '3.5'
is converted to the double 3.5
. The num
type lets you work with either kind of number easily.
Fun Example: Adding Numbers from Strings
Sometimes, numbers come as strings and you need to add them as real numbers, not as text. You can do this by converting the strings to numbers first.
Here’s a fun example where we add the strings "7"
and "8"
as numbers:
void main() {
String str1 = '7';
String str2 = '8';
int sum = int.parse(str1) + int.parse(str2);
print('Sum is $sum');
}
In this code, both strings are converted to integers using int.parse()
. Then, they are added together, and the result is printed. This way, you get the number 15
, not the text "78"
.
Using Try-Catch to Avoid Errors
When converting strings to numbers, sometimes the string may not be a valid number. To avoid your program crashing, you can use try-catch
to handle these cases safely.
Here is an example where we try to convert a string to an integer. If the string is not a number, we catch the error and print a message:
void main() {
String badInput = 'hello';
try {
int number = int.parse(badInput);
print('Number is $number');
} catch (e) {
print('Cannot convert "$badInput" to number');
}
}
This way, if the input is not a valid number, the program doesn’t stop. Instead, it tells you it can’t convert the string, keeping your app safe and working.
Using tryParse()
for Safe Conversion Without Exceptions
Another easy way to convert strings to numbers safely is by using the tryParse()
method. Unlike parse()
, tryParse()
returns null
if the string is not a valid number, so it doesn’t throw an error.
Here’s how you can use tryParse()
to convert a string to an integer safely:
void main() {
String input = 'hello';
int? number = int.tryParse(input);
if (number == null) {
print('Cannot convert "$input" to number');
} else {
print('Number is $number');
}
}
With tryParse()
, you just check if the result is null
. If it is, the conversion failed, and you can handle it smoothly without using try-catch
. This makes your code cleaner and easier to read.
Conclusion
In this article, we learned several ways to convert strings to numbers in Dart. You can use int.parse()
to convert strings to integers and double.parse()
for decimal numbers. For flexible cases, num.parse()
works for both integers and doubles. When dealing with extra spaces, remember to trim the string first. To avoid errors, you can use try-catch
or the safer tryParse()
method, which returns null if conversion fails. These methods help you turn string data into numbers easily and safely in your Dart programs.