The `parse` method is used to parse a string as a double literal and return its numerical value.
The syntax of double.parse() static-method is:
double parse(String source, [ double onError(String source) ])
This parse() static-method of double parse source
as an double literal and return its value.
Parameter | Optional/Required | Description |
---|---|---|
source | required | the string to parse as a double |
onError | optional | a function that returns a default value if parsing fails |
double.parse() returns value of type double
.
In this example,
value1
with the value '3.14'
.double.parse()
method to parse value1
as a double.result1
.void main() {
String value1 = '3.14';
double result1 = double.parse(value1);
print('Result 1: $result1');
}
Result 1: 3.14
In this example,
value2
with the value '10'
.double.parse()
method to parse value2
as a double.result2
.void main() {
String value2 = '10';
double result2 = double.parse(value2);
print('Result 2: $result2');
}
Result 2: 10
In this example,
value3
with the value 'abc'
.double.parse()
method to parse value3
as a double.value3
cannot be parsed as a double, we provide a custom error handling function that returns 0.0
.result3
.void main() {
String value3 = 'abc';
double result3 = double.parse(value3);
print('Result 3: $result3');
}
Script error.
In this Dart tutorial, we learned about parse() static-method of double: the syntax and few working examples with output and detailed explanation for each example.