Numbers and Math
Int and Double Arithmetic
Dart has int for whole numbers and double for IEEE-754 fractional values. Mixing the two widens the result to a double. / always returns a double; use ~/ for integer division. toInt() truncates a double toward zero, and toDouble() widens an int.
Program
Play the program to widen int + double, convert values in both numeric directions, and watch / produce a double from two int operands.
int_double.dart
void main() {
int a = 7;
double b = 2.5;
var sum = a + b;
var asInt = b.toInt();
var asDouble = a.toDouble();
var quotient = a / 2;
print('$sum $asInt $asDouble $quotient');
}
widening
`int + double` evaluates to a `double`-valued result; the `int` is widened to match.
/
`/` returns a `double` even when both operands are `int`. Use `~/` for an integer quotient.
conversions
`double.toInt()` truncates toward zero; `int.toDouble()` widens a whole number to a `double`.