This tutorial shows multiple ways to convert double to String or string to double in Dart and flutter programming language.
The string
is a string of numbers or characters enclosed in double quotes or single quotes.
A double is a primitive type of floating-point number in Dart programming
Both are of different types, Automatic conversions are not possible. So, We have to manually Convert String into double or double to String with examples
It is easy to parse String to Int in Dart with inbuilt methods
How to parse String to double in Dart programming example
There are multiple ways we can do the conversion.
- use parse method in a double class
double
class provides static parse()
method, takes an input string, and converts into double type.
Syntax:
double parse(String str)
Argument
is a string of a number, if the string contains the non-numeric value, It throws
Uncaught Error: FormatException: Invalid double
Here is an example
void main() {
var str = '123.10';
var number = double.parse(str);
print(number); // 123.1
print(number.runtimeType); // double
print(number == 123.10); // true
}
parse
method throws an error for an invalid string of floating numbers such as non-numeric characters and null.
The below parse
method throws Uncaught Error: FormatException: Invalid double
123.10d
void main() {
var str='123.1a0d';
var number = double.parse(str);
print(number.runtimeType); // double
print(number==123.10); // true
}
-
double tryParse method
tryParse
in the double class is advanced to theparse
method.It handles runtime exceptions and returns a null if you passed a string of non-numeric invalid numbers.
Here is an double tryParse method example for successful usecase
void main() {
var str = '123.10';
var number = double.tryParse(str);
print(number); // 123.1
print(number.runtimeType); // double
print(number == 123.10); // true
}
The below example return null and handles invalid string floating values
void main() {
var str = '123a.10';
var number = double.tryParse(str);
print(number); // null
print(number.runtimeType); // Null
print(number == 123.10); // false
}
parse
and tryParse()
both methods can be used in flutter programming for Converting string to double.
How to Convert Double to String in Dart or flutter for example?
There are multiple ways to convert double to String in dart.
-
toString()
-
toRadixString();
-
Append string with interpolation syntax
-
toString() method
Every class in dart provides the toString()
method.
double
class provides this toString()
method and return string version of a floating number.
void main() {
double number = 12.11;
String str = number.toString();
print(str); // 12.11
print(str.runtimeType); // String
print(str == '12.11'); // true
}
- append a string with interpolation syntax
Append a string can be done with interpolation syntax.
String interpolation
(${}) enclosed in single or double-quotes.
You can append strings or plain numbers with interpolation syntax.
Here is an example
void main() {
double number = 12.11;
String str = '${number}';
print(str); // 12.11
print(str.runtimeType);// String
print(str == "12.11"); // true
}
Convert long double to string without scientific notation in Dart and flutter
Sometimes, you have a double floating number with a long floating value, which needs to convert to String.
Below examples uses toString()
and string interpolation syntax
and print the string with scientific noation as output.
void main() {
double number = 0.00000000124211113245266;
String numToString = number.toString();
String str = '${number}';
print(str); // 1.24211113245266e-9
print(numToString); // 1.24211113245266e-9
}
Output:
1.24211113245266e-9
1.24211113245266e-9
To convert a string without scientific notation, You can do it with toStringAsFixed() method or decimal parse method
- toStringAsFixed() method
Convert the long double to string with a limit of 20 fraction digits with this method.
String toStringAsFixed(int fractionDigits)
Here is an toStringAsFixed example
void main() {
double number = 0.00000000124211113245266;
String numToString = number.toStringAsFixed(20);
print(numToString); // 0.00000000124211113245
}
The only disadvantage with this is limit is 20 fractionDigits, If you supply more than 20 digits, It throws below error Uncaught Error: RangeError (fractionDigits): Invalid value: Not in inclusive range 0..20: 23
Next, use decimal package
the decimal package provides robust handling numbers.
- Import decimal package
- Convert to Decimal from double using Decimal.parse() method
- Finally, Convert decimal to String with append syntax
Here is an example code
import 'package:decimal/decimal.dart';
void main() {
double number = 0.00000000124211113245266;
Decimal decimal = Decimal.parse(number.toString());
String str = '${number}';
print(str); // 0.00000000124211113245266
}
Conclusion
To summarize, Learned how to convert string to double vice versa in Dart language with examples.
And, also how to convert long double fractions into a string without scientific notification