Home
Map
Math.Abs: Absolute ValueUse the Math.Abs Function. Abs computes absolute values for Integers, Doubles and other types.
VB.NET
This page was last reviewed on Oct 26, 2021.
Math.Abs. This VB.NET function returns the absolute value of a number. In mathematics the absolute value of a number is positive.
Function info. The numbers -10 and 10 both have the absolute value of 10. In VB.NET, the Math.Abs function provides this capability.
Example. This program uses Integer and Double types with the Math.Abs function. Only the negative inputs have a different absolute value.
Info The value -1000 has an absolute value of 1000. And -100.123 has an absolute value of 100.123.
Integer
Double
Tip The value returned from Math.Abs is of the same type as the value you pass to it.
So If you pass a Double as the argument to Math.Abs, it also returns a Double.
Module Module1 Sub Main() ' Absolute values of integers. Dim value1 As Integer = -1000 Dim value2 As Integer = 20 Dim abs1 As Integer = Math.Abs(value1) Dim abs2 As Integer = Math.Abs(value2) Console.WriteLine(value1) Console.WriteLine(abs1) Console.WriteLine(value2) Console.WriteLine(abs2) ' Absolute values of doubles. Dim value3 As Double = -100.123 Dim value4 As Double = 20.2 Dim abs3 As Double = Math.Abs(value3) Dim abs4 As Double = Math.Abs(value4) Console.WriteLine(value3) Console.WriteLine(abs3) Console.WriteLine(value4) Console.WriteLine(abs4) End Sub End Module
-1000 1000 20 20 -100.123 100.123 20.2 20.2
Internals. The internal code of Math.Abs depends on the value type. On some types, such as Integer, the function simply tests for a negative number and then negates it.
Also For types such as Decimal and Double, other methods are internally called.
Summary. Computing absolute values of a number is useful in many programs. It is useful even in functions that compute hashes. The .NET Math.Abs function is convenient and well-tested.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 26, 2021 (image).
Home
Changes
© 2007-2024 Sam Allen.