How to convert Double to String or String to double in Dart| Flutter By Example
This tutorial shows multiple ways to convert double to String or string to double in Dart and flutter programming languages.
Convert Double to String in Dart:
Double.parse
method takesstring
number and returns adouble
value.double.parse("111.12")
returns111.12
. ThrowsFormatException
if given string isnon-numeric
.
Double.tryParse
method takesstring
number and returnsdouble
value.double.tryParse("111.12")
returns111.12
. Does not throw exception and returnsnull
if the string isnon-numeric
.Convert String to Double in Dart: -
Double toString()
method returns thestring
version of adouble
number.123.11.toString()
returns123.11
.toRadixString
() convert string to double.Append string with interpolation
${}
syntax.'${number}'
where number is adouble
number.
Let’s explore the String and Double types in Dart and Flutter.
A string
is a sequence of characters enclosed in either double quotes or single quotes. It can consist of numbers, letters, symbols, or a combination of these. Example string is ‘1.123’.
On the other hand, a double is a primitive data type representing a floating-point number in Dart programming. Example double is ‘1.123’.
Both types are distinct, and automatic conversions between them are not possible. Therefore, manual conversion is required when converting from a String to a double or vice versa.
Here are some examples illustrating how to perform these conversions.
Check my other posts
How to parse String to double in Dart programming example
There are multiple ways to perform this conversion. One common approach is to use the parse
method in the double
class.
use the parse method in a double class
The
double
class provides a staticparse()
method, which takes an input string and converts it into a double type.Syntax:
double parse(String str)
The
argument
is a string representation of a number. If the string contains non-numeric characters, it throws a 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 }
The
parse
method throws an error for invalid string representations of floating-point numbers, such as those containing non-numeric characters or null values.For example, attempting to parse the string
123.1a0d
will result in a 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
The
tryParse
method in the double class provides a more robust alternative to theparse
method.It efficiently handles runtime exceptions and returns null if the provided string is invalid or contains non-numeric characters.
Here is a 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 above example demonstrates the successful parsing of a valid string into a double.
The below example returns 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 }
In this example, the tryParse method returns null when the input string contains invalid floating-point values, ensuring smooth error handling without raising exceptions.
parse
and tryParse()
both methods can be used in flutter programming for Converting strings 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() method
toRadixString() method
Append string with interpolation syntax
using toString() method
In Dart, every class provides the
toString()
method, which returns a string representation of the object.The
double
class also provides thetoString()
method, which converts a floating-point number into its string representation.void main() { double number = 12.11; String str = number.toString(); print(str); // 12.11 print(str.runtimeType); // String print(str == '12.11'); // true }
In this example, the
toString()
method is used to convert the double number 12.11 into its string representation. The resulting string is then printed along with its type information.Appending a String with Interpolation Syntax
String appending can be achieved using interpolation syntax, denoted by
${}
within single or double quotes. This allows for dynamic inclusion of variables or expressions within a string.You can append strings or plain numbers using 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 }
In this example, the interpolation syntax
${number}
is used within the string to dynamically append the value of the variable number.The resulting string is then printed along with its type information.
Convert Long Double to String without Scientific Notation in Dart and Flutter
Sometimes, you may encounter a double floating-point number with an extended floating value that needs to be converted to a string without scientific notation.
Let’s explore various methods to achieve this in Dart and Flutter.
Using toString() and String Interpolation Syntax
The
toString()
method and string interpolation syntax can be used to convert a long double to a string. However, by default, they may output the string with scientific notation.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
Using toStringAsFixed() Method
To avoid scientific notation and retain precision, you can utilize the
toStringAsFixed()
method, which allows specifying the number of fraction digits.String toStringAsFixed(int fractionDigits)
Here is a toStringAsFixed example
void main() { double number = 0.00000000124211113245266; String numToString = number.toStringAsFixed(20); print(numToString); // 0.00000000124211113245 }
The limitation of toStringAsFixed()
is that it only supports up to 20 fraction digits. Attempting to use more than 20 digits will result in a below error.
Uncaught Error: RangeError (fractionDigits): Invalid value: Not in inclusive range 0..20: 23
Next, use a decimal package
Using the Decimal Package
For more robust handling of numbers, you can utilize the
decimal
package.- 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
In this tutorial, we’ve learned various methods to convert strings to doubles and vice versa in Dart. Additionally, we explored techniques to convert long double fractions into strings without scientific notation, ensuring precision and clarity in our numeric representations.