Dart parse() function
Dart parse() function
Let’s learn about the Dart parse() function in this tutorial. There are different methods to use the parse() function depending on the type of number you want to get as an output.
Dart parse() Function
The Dart parse() function converts the numeric string to the number.
int.parse()
We can use the int.parse() method to parse a string as an integer. This method takes a string as a parameter and returns an int value.
double.parse()
To parse a string as a double, you can use the double.parse() method. This method takes a string as a parameter and returns a double value.
Example
/*
* parse() function demo Program
* Dart Tutorials – www.TestingDocs.com
*/
void main(){
// convert String to double number
double x =double.parse(“17.96”);
double y =double.parse(“28.54”);
double z = x + y;
print(“The sum of the x, y = ${z}”);
}
In this example, we have converted two strings to double numbers.
We have performed add operation on the numbers and printed the output
to the console screen.
num.parse()
To parse a string as a num, which is the superclass of int and double, you can use the num.parse() method.
Example
Let’s use the num.parse() method in this example.
var pi_approx = num.parse(“3.14”);
In this example, we have converted the string “3.14” into a number by using the parse() method and then stored it in the variable named pi_approx.
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/