{

How to convert Double to String or String to double in Dart| Flutter By Example


Convert Double to String and vice versa  in Dart or Flutter

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 takes string number and returns adouble value. double.parse("111.12") returns 111.12. Throws FormatException if given string is non-numeric.
  • Double.tryParse method takes string number and returns double value. double.tryParse("111.12") returns 111.12. Does not throw exception and returns null if the string is non-numeric.

  • Convert String to Double in Dart: -Double toString() method returns the string version of a double number. 123.11.toString() returns 123.11.

  • toRadixString() convert string to double.

  • Append string with interpolation ${} syntax.'${number}' where number is a double number.

let’s see what are String and Double types in Dart and Flutter.

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

Check my other posts

How to parse String to double in Dart programming example

There are multiple ways we can do the conversion.

  • use the parse method in a double class

The double class provides a static parse() method, takes an input string, and converts it into a double type.

Syntax:

double parse(String str)

Argument is a string of a number, if the string contains a 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
}

The 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 the parse method.

    It handles runtime exceptions and returns a null if you passed a string of non-numeric invalid numbers.

    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 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
}

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

Every class in dart provides the toString() method.

The double class provides this toString() method and returns the 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 a String.

The below examples use toString() and string interpolation syntax and print the string with scientific notation 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 a toStringAsFixed example

void main() {
  double number = 0.00000000124211113245266;
  String numToString = number.toStringAsFixed(20);
  print(numToString); // 0.00000000124211113245

}

The only disadvantage with this is the limit is 20 fraction digits, 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 a 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 and vice versa in Dart language with examples.

And, also how to convert long double fractions into a string without scientific notification.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not