Dart: Number Absolute Value

Sometimes in programming, we don’t care whether a number is negative or positive — we just want to know how far it is from zero. That’s where absolute value comes in.

What is Absolute Value?

In simple terms, the absolute value of a number is how far it is from zero, ignoring the sign. So both 5 and -5 have an absolute value of 5.

Think about temperature. Whether it’s -5°C or 5°C, both are 5 degrees away from zero. The absolute value helps us measure that distance without worrying about whether it’s above or below zero.

Why Use Absolute Value?

Absolute value is super useful when:

  • You’re working with distances, and direction doesn’t matter.
  • You’re finding differences between numbers.
  • You want to make sure numbers are always positive before using them.

In Dart, getting the absolute value is simple — and that’s what this guide is all about!

Using .abs() in Dart

Dart makes it super easy to get the absolute value of a number. Just use the .abs() method — like giving the number a quick makeover to remove any negative signs!

Basic Syntax:

number.abs()

This works with both integers and decimal numbers (doubles).

Example: Getting the Absolute Value of a Number

Let’s say you have a number that might be negative, and you want to turn it into a positive number. In Dart, every number has an .abs() method that returns its absolute value.

void main() {

  var num = -8;
  print(num.abs()); // Output: 8

}

In this example, -8.abs() gives us 8. It simply removes the minus sign and makes the number positive. Very useful when you only care about the size of the number, not the direction!

Absolute Value with Integers

When you’re working with whole numbers (called int in Dart), you can use .abs() to make sure the value is always positive.

void main() {

  int steps = -1200;
  print(steps.abs()); // Output: 1200

}

Imagine you’re building a fitness app that tracks steps. Even if the data somehow shows -1200, what really matters is the number of steps, not the sign. Using .abs() helps you keep the number positive and useful.

Absolute Value with Doubles

You can also use .abs() with numbers that have decimal points — these are called double in Dart.

void main() {

  double temperatureDifference = -3.6;
  print(temperatureDifference.abs()); // Output: 3.6

}

Let’s say you’re showing how much the temperature has changed. Whether it dropped by -3.6 degrees or rose by 3.6, the amount of change is the same. Using .abs() shows the temperature difference without worrying about whether it went up or down.

Storing Absolute Values

Sometimes you don’t just want to print the absolute value — you want to keep it for later use.
You can store it in a new variable using .abs().

void main() {

  var speed = -45;
  var safeSpeed = speed.abs();

  print(safeSpeed); // Output: 45

}

Maybe you’re working with speed, and you only care about how fast something is going — not the direction. Storing the absolute value in a variable helps you keep things clean and easy to reuse later in your code.

Using Absolute Value in Expressions

You can use .abs() as part of a bigger calculation. This is great when you want the result to always be positive — no matter the order of the numbers.

void main() {

  var x = -10, y = 4;
  var result = (x - y).abs();

  print(result); // Output: 14

}

Perfect for situations like measuring time gaps or distances, where you only care about how far apart two things are — not which one is bigger.

Real-World Example: Distance Between Points

Sometimes, you want to find the distance between two numbers — like two places on a number line.

Getting the gap between two points, no matter which one is bigger.

void main() {

  int pointA = 3, pointB = 10;
  int distance = (pointA - pointB).abs();

  print(distance); // Output: 7

}

By using .abs(), you make sure the result is always positive. That way, you’re measuring how far apart the points are, not the direction.

Absolute Value in Conditional Logic

You can use .abs() inside if statements to make decisions based on how far a number is from zero, without caring if it’s negative or positive.

void main() {

  var value = -9;

  if (value.abs() > 5) {
    print("Value is far from zero!");
  }

}

This is helpful when you only care about how big the number is, not whether it’s positive or negative—like checking if a reading is too far off from zero.

Conclusion

Using .abs() in Dart is a quick and easy way to get the positive version of any number. Whether you’re dealing with steps, distances, or differences, absolute value helps you focus on size—not direction.

Whenever the sign doesn’t matter, and you only care about “how much,” use .abs() to keep things simple and clear.