Dart Numbers
Dart has two number types — int and double — and their shared supertype num. Numbers are objects, so they come with handy methods and properties.
int and double
void main() {
int items = 12;
int hex = 0x1F;
int big = 1_000_000; // digit separators (Dart 3.6+)
double price = 19.99;
double sci = 2.5e3;
num either = 7; // num can hold int or double
either = 7.5;
print([items, hex, big, price, sci, either]);
double d = 3; // an int literal becomes a double here
print(d);
}[12, 31, 1000000, 19.99, 2500.0, 7.5]
3.0
| Type | Size | Range / precision |
|---|---|---|
int |
64-bit (native) | −2⁶³ to 2⁶³−1 |
double |
64-bit IEEE 754 | ~15–17 significant digits |
Note: When compiled to JavaScript,
intvalues are JavaScript numbers, so integers are only exact up to 2⁵³. On the native VM they are full 64-bit integers that wrap on overflow.
Arithmetic
| Operator | Meaning | 7 ? 2 |
|---|---|---|
+ |
Add | 9 |
- |
Subtract | 5 |
* |
Multiply | 14 |
/ |
Divide — always returns a double |
3.5 |
~/ |
Integer (truncating) divide | 3 |
% |
Remainder (always non-negative for positive divisor) | 1 |
void main() {
print(7 / 2);
print(8 / 2); // still a double
print(7 ~/ 2);
print(-7 ~/ 2); // truncates toward zero
print(-7 % 2); // Dart's % is never negative here
print((-7).remainder(2)); // sign follows the dividend
print(0.1 + 0.2);
}3.5
4.0
3
-3
1
-1
0.30000000000000004
0.1 + 0.2 isn't exactly 0.3 — that's binary floating point, the same in every language. Use integers (e.g. cents) for money.
Useful properties and methods
Numbers have methods such as abs(), clamp() and toRadixString(). Math functions like pow, sqrt and max are not methods — they live in the dart:math library, which you import at the top of the file:
import 'dart:math';
void main() {
var n = -17;
print(n.abs());
print(n.isOdd);
print(10.clamp(0, 5));
print(255.toRadixString(16));
print(pow(2, 10));
print(sqrt(144));
print(max(3, 8));
print(pi.toStringAsFixed(4));
}17
true
5
ff
1024
12.0
8
3.1416
Rounding doubles
| Method | 2.5 |
-2.5 |
2.4 |
|---|---|---|---|
round() |
3 |
-3 |
2 |
floor() |
2 |
-3 |
2 |
ceil() |
3 |
-2 |
3 |
truncate() |
2 |
-2 |
2 |
All four return an int. For a formatted string, use toStringAsFixed:
void main() {
var total = 1234.5678;
print(total.round());
print(total.floor());
print(total.toStringAsFixed(2));
print(total.toStringAsPrecision(3));
print((2.5).round());
print((-2.5).round());
print(double.nan.isNaN);
print(1 / 0);
}1235
1234
1234.57
1.23e+3
3
-3
true
Infinity
Parsing numbers safely
int.parse throws a FormatException on bad input; int.tryParse returns null. Combine tryParse with ?? for a default:
void main() {
for (final input in ['42', ' 7 ', '3.9', 'abc']) {
final value = int.tryParse(input.trim()) ?? -1;
print('"$input" -> $value');
}
print(num.parse('3.9'));
}"42" -> 42
" 7 " -> 7
"3.9" -> -1
"abc" -> -1
3.9
Test yourself
What is the result of 9 / 3 in Dart?
- 3
- 3.0
- 3.0000001
- Compile error
Which operator performs integer division?
- //
- div
- ~/
- \
Where do sqrt, pow and pi come from?
- dart:core
- dart:math
- dart:num
- They are methods on int