Dart Numbers
Dart Numbers
Dart has two types of numbers:
intfor whole numbers, like7,-150or2024doublefor numbers with decimals, like3.5or-0.25
Both types belong to a common type called num.
int
An int is a whole number, positive or negative, without decimals:
Example
void main() {
int apples = 12;
int temperature = -5;
int population = 8000000000;
print(apples);
print(temperature);
print(population);
}
Try it Yourself »
Result:
12
-5
8000000000
You cannot store a number with decimals in an int variable:
Result:
main.dart:2:15: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
int price = 9.99;
^
double
A double is a number with decimals:
Example
void main() {
double price = 9.99;
double height = 1.75;
double rate = 2;
print(price);
print(height);
print(rate);
}
Try it Yourself »
Result:
9.99
1.75
2.0
Note that rate was given the whole number 2, but because it is a double, Dart prints it as 2.0.
A double can also be written in scientific notation, with an e that means "times 10 to the power of":
Example
void main() {
double big = 1.5e3;
double small = 2e-4;
print(big);
print(small);
}
Try it Yourself »
Result:
1500.0
0.0002
num
A variable of type num can hold an int or a double:
Result:
10
9.5
When you divide two numbers with /, the result is always a double.
To get a whole number result, use ~/ instead:
Result:
5.0
3.5
3
You will learn more about math operators in the Arithmetic chapter.
Number Properties and Methods
Numbers in Dart have built-in properties (values you can read) and methods (actions you can call).
You use them with a dot after the number or the variable:
Example
void main() {
int a = -8;
double b = 4.6;
print(a.abs());
print(b.round());
print(b.floor());
print(b.ceil());
print(a.isEven);
print(a.isNegative);
}
Try it Yourself »
Result:
8
5
4
5
true
true
Use toStringAsFixed() to show a number with a fixed number of decimals, for example a price:
Example
void main() {
double total = 19.987;
print(total.toStringAsFixed(2));
print(total.toStringAsFixed(0));
}
Try it Yourself »
Result:
19.99
20
This table lists some common number properties and methods:
| Property or method | Description | Example | Result |
|---|---|---|---|
abs() | Returns the number without its minus sign | (-8).abs() | 8 |
round() | Rounds to the nearest whole number | 4.6.round() | 5 |
floor() | Rounds down | 4.6.floor() | 4 |
ceil() | Rounds up | 4.2.ceil() | 5 |
toInt() | Removes the decimals and returns an int | 4.9.toInt() | 4 |
toDouble() | Returns the number as a double | 5.toDouble() | 5.0 |
toStringAsFixed(n) | Returns a string with n decimals | 3.14159.toStringAsFixed(2) | 3.14 |
isEven | true if an int is even | 4.isEven | true |
isOdd | true if an int is odd | 7.isOdd | true |
isNegative | true if the number is less than zero | 5.isNegative | false |
Note: To call a method on a negative number, put the number in parentheses, like (-8).abs().
Without them, Dart reads -8.abs() as -(8.abs()), which is -8.
Parse Numbers From Text
Sometimes a number is stored as text, for example when a user types it.
Use int.parse() or double.parse() to turn the text into a number:
Example
void main() {
var a = int.parse('42');
var b = double.parse('3.5');
print(a + 8);
print(b * 2);
}
Try it Yourself »
Result:
50
7.0
You will learn more in the Type Conversion chapter.