Skip to main content
The % operator in Dart is the arithmetic modulo operator, which calculates the Euclidean remainder of a division operation between two numbers. Unlike the modulo operator in many C-style languages (which perform a truncating remainder), Dart’s % operator strictly enforces Euclidean division, guaranteeing that the returned remainder is always non-negative regardless of the operands’ signs.

Type Evaluation

The static and runtime return type of the % operation is determined by the types of its operands:
  • If both operands are of type int, the operation yields an int.
  • If at least one operand is of type double, the operation yields a double.

Euclidean Modulo Behavior

Dart calculates the modulo such that the result r always satisfies the mathematical condition 0 <= r < |divisor|. Because it is a Euclidean modulo, negative operands will not produce a negative remainder.
Note: If you require a truncating remainder operation where the result retains the sign of the dividend, Dart provides the num.remainder() method as the strict alternative to the % operator.

Exceptional Cases

The behavior of the % operator when the divisor is zero diverges based on the operand types:
  • Integer Division by Zero: Evaluating int % 0 throws an UnsupportedError (Integer division by zero).
  • Floating-Point Division by Zero: Evaluating double % 0 or num % 0.0 does not throw. Instead, it evaluates to NaN (Not a Number) in compliance with IEEE 754 floating-point standards.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More