Dart Double
last modified June 4, 2025
In Dart, double is a built-in type representing 64-bit floating-point numbers. It conforms to the IEEE 754 standard for floating-point arithmetic.
Double values can represent both integer and fractional numbers. They are suitable for scientific calculations and measurements requiring precision.
Basic Double Declaration
The simplest way to create a double is by using literal notation.
void main() {
double temperature = 23.5;
double price = 9.99;
double scientific = 1.23e5; // 1.23 × 10^5
print(temperature);
print(price);
print(scientific);
}
We declare three double variables with different values. Scientific notation can be used for very large or small numbers.
$ dart main.dart 23.5 9.99 123000.0
Double Operations
Doubles support standard arithmetic operations like addition and division.
void main() {
double x = 10.5;
double y = 3.2;
print('Addition: ${x + y}');
print('Subtraction: ${x - y}');
print('Multiplication: ${x * y}');
print('Division: ${x / y}');
print('Remainder: ${x % y}');
print('Power: ${x * x}');
}
This demonstrates basic arithmetic with double values. Note that division always returns a double, even for whole number results.
$ dart main.dart Addition: 13.7 Subtraction: 7.3 Multiplication: 33.6 Division: 3.28125 Remainder: 0.9000000000000004 Power: 110.25
Double Parsing
We can convert strings to doubles using parse and
tryParse methods.
void main() {
String numStr = '3.14159';
String invalidStr = '3.14.15';
double pi = double.parse(numStr);
double? parsed = double.tryParse(invalidStr);
print('Parsed pi: $pi');
print('Try parse invalid: $parsed');
}
parse throws FormatException for invalid input, while
tryParse returns null. Always use tryParse when
dealing with user input.
$ dart main.dart Parsed pi: 3.14159 Try parse invalid: null
Double Comparison
Comparing doubles requires special care due to floating-point precision.
void main() {
double a = 0.1 + 0.2;
double b = 0.3;
print('Direct comparison: ${a == b}');
print('Precise comparison: ${(a - b).abs() < 1e-10}');
print('a: $a');
print('b: $b');
}
Due to binary representation, 0.1 + 0.2 isn't exactly 0.3. We use an epsilon comparison for reliable results with floating-point numbers.
$ dart main.dart Direct comparison: false Precise comparison: true a: 0.30000000000000004 b: 0.3
Double Methods
The double type provides useful methods for rounding and conversion.
void main() {
double value = 3.14159265359;
print('toStringAsFixed(2): ${value.toStringAsFixed(2)}');
print('toStringAsExponential(2): ${value.toStringAsExponential(2)}');
print('toStringAsPrecision(4): ${value.toStringAsPrecision(4)}');
print('round(): ${value.round()}');
print('floor(): ${value.floor()}');
print('ceil(): ${value.ceil()}');
print('truncate(): ${value.truncate()}');
}
These methods help format and convert double values. toStringAsFixed is particularly useful for displaying currency values.
$ dart main.dart toStringAsFixed(2): 3.14 toStringAsExponential(2): 3.14e+0 toStringAsPrecision(4): 3.142 round(): 3 floor(): 3 ceil(): 4 truncate(): 3
Best Practices
- Precision Handling: Be aware of floating-point precision limitations.
- Comparisons: Use epsilon comparisons instead of direct equality.
- Parsing: Prefer tryParse over parse for user input.
- Formatting: Use toStringAsFixed for consistent decimal places.
Source
This tutorial covered Dart's double type with practical examples demonstrating its features and common usage patterns.
Author
List all Dart tutorials.