Search⌘ K
AI Features

Working with Numbers

Explore how to work with Dart's core numeric types by mastering the use of int for whole numbers, double for decimals and scientific notation, and num for flexible number handling. Understand automatic conversions and best practices for type-safe code.

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 two specific ...