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>.
Declaration and Assignment
Variables declared with the typenum exhibit polymorphic behavior regarding numeric values. A variable typed as num can be reassigned between integer and floating-point values dynamically.
Arithmetic and Comparison Operations
Thenum 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.
<<, &, |) are not defined in num; they are specific to the int subclass.
Key Properties and Methods
Thenum class exposes properties and methods for numeric manipulation and inspection.
Properties:
isNaN: Returnstrueif the number is the Double Not-a-Number value.isNegative: Returnstrueif the number is less than zero.isInfinite: Returnstrueif the number is positive or negative infinity.
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 withndigits of precision.
Platform Implementation Details
Whilenum provides a unified API, the underlying representation depends on the compilation target:
- Dart Native (VM/AOT):
numreferences either a 64-bit two’s complement integer (int) or a 64-bit IEEE 754 floating-point number (double). - Dart Web (JavaScript Compilation): Both
intanddoublemap to JavaScriptNumber(double-precision floating-point). Dart enforcesintsemantics (such as truncation) where necessary, but the underlying storage is floating-point. - Dart Web (WebAssembly Compilation): Dart maps
intto Wasm 64-bit integers (i64) anddoubleto Wasm 64-bit floats (f64).
Master Dart with Deep Grasping Methodology!Learn More





