Search⌘ K
AI Features

Working with Numbers

Explore how to declare and use Dart's numeric data types including int for whole numbers, double for decimals, and the versatile num type. Understand when to use each subtype, including hexadecimal and scientific notation. Gain skills to write clear, type-safe numeric assignments and output in Dart.

The most common data types we work with in programming are numbers. If we want a variable to hold a numeric value, we can declare it using the general num data type.

Let us start by creating a simple variable using the num type and assigning a basic number literal to it.

Dart
void main() {
num age = 25;
print(age);
}
  • Line 2: We use the flexible num data type to reserve memory for a numeric value. We name this container age and store the whole number 25 inside it, though the num type is versatile enough to also hold decimal values.

  • Line 3: We retrieve the data currently stored in the age variable and output it to the console using the print function.

Subtypes of numbers

While num is useful as a general container, Dart numbers are further divided into ...