Absolute value
The absolute value tells you how far a number is from zero.
Math.Abs
Simple example
Start−3
Result3
using System;
Console.WriteLine(Math.Abs(-3));
3
Advertisements
Everyday examples
It is −6 degrees outside. How many degrees below zero is that?
Start−6 → 0
Result6
using System;
int temperature = -6;
Console.WriteLine(Math.Abs(temperature));
6
The target is 15 points and the score is 12. How far apart are they?
Start12 − 15
Result3
using System;
int score = 12;
int target = 15;
Console.WriteLine(Math.Abs((score - target)));
3
A lift goes from floor 5 to floor 2. How many floors does it travel?
Start5 → 2
Result3
using System;
int start = 5;
int finish = 2;
Console.WriteLine(Math.Abs((finish - start)));
3
Use small signed numbers here. The result is the distance from zero.
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson