Skip to main content
num is an abstract class in the dart:core library that serves as the superclass for all numeric types in Dart. It defines the shared interface, arithmetic operators, and comparison logic for its two concrete subclasses: int (integers) and double (floating-point numbers).

Type Hierarchy

As an abstract type, num cannot be instantiated directly. It functions as a compile-time type annotation that permits a variable to hold either an int or a double at runtime. It implements Comparable<num>.
Object
 └── num
      ├── int
      └── double

Declaration and Assignment

Variables declared with the type num exhibit polymorphic behavior regarding numeric values. A variable typed as num can be reassigned between integer and floating-point values dynamically.
void main() {
  // Compile-time type is num; Runtime type is int
  num value = 10;
  print(value.runtimeType); // Output: int

  // Reassignment to double is valid for type num
  value = 12.5;
  print(value.runtimeType); // Output: double
}

Arithmetic and Comparison Operations

The num class defines standard arithmetic operators (+, -, *, %) with the signature num operator (num other). While the runtime value may be an int or double depending on the operands, the static return type of these operations when invoked on a num variable is num. Exceptions to this rule are standard division (/), which always returns double, and truncating division (~/), which always returns int.
num x = 10;    // int
num y = 2.5;   // double

// The static type of 'sum' is num.
// The runtime value is 12.5 (double).
num sum = x + y;

// Standard division always returns double
double quotient = x / 2;

// Truncating division always returns int
int truncated = x ~/ y;
Note: Bitwise operators (e.g., <<, &, |) are not defined in num; they are specific to the int subclass.

Key Properties and Methods

The num class exposes properties and methods for numeric manipulation and inspection. Properties:
  • isNaN: Returns true if the number is the Double Not-a-Number value.
  • isNegative: Returns true if the number is less than zero.
  • isInfinite: Returns true if the number is positive or negative infinity.
Methods:
  • abs(): Returns the absolute value.
  • ceil(), floor(), round(): Returns the least integer no smaller than, greatest integer no larger than, or the integer closest to the number, respectively.
  • clamp(lower, upper): Returns the number constrained to the given range.
  • toInt(): Truncates the number to an integer.
  • toDouble(): Returns the number as a double.
  • toStringAsFixed(n): Returns a decimal-point string representation with n digits of precision.

Platform Implementation Details

While num provides a unified API, the underlying representation depends on the compilation target:
  1. Dart Native (VM/AOT): num references either a 64-bit two’s complement integer (int) or a 64-bit IEEE 754 floating-point number (double).
  2. Dart Web (JavaScript Compilation): Both int and double map to JavaScript Number (double-precision floating-point). Dart enforces int semantics (such as truncation) where necessary, but the underlying storage is floating-point.
  3. Dart Web (WebAssembly Compilation): Dart maps int to Wasm 64-bit integers (i64) and double to Wasm 64-bit floats (f64).
Master Dart with Deep Grasping Methodology!Learn More