Library reference

Absolute value

The absolute value tells you how far a number is from zero.

int.abs

Simple example

Start−3
Result3
lesson.dart
void main() {
    print((-3).abs());
}
Output
3
Advertisements

Everyday examples

It is −6 degrees outside. How many degrees below zero is that?

Start−6 → 0
Result6
lesson.dart
void main() {
    final temperature = -6;
    print((temperature).abs());
}
Output
6

The target is 15 points and the score is 12. How far apart are they?

Start12 − 15
Result3
lesson.dart
void main() {
    final score = 12;
    final target = 15;
    print(((score - target)).abs());
}
Output
3

A lift goes from floor 5 to floor 2. How many floors does it travel?

Start5 → 2
Result3
lesson.dart
void main() {
    final start = 5;
    final finish = 2;
    print(((finish - start)).abs());
}
Output
3

Use small signed numbers here. The result is the distance from zero.

Run the example
Terminal
dart run lesson.dart
Official documentation