Math.Abs Metod

Definition

Returnerar det absoluta värdet för ett angivet tal.

Överlagringar

Name Description
Abs(Decimal)

Returnerar det absoluta värdet för ett Decimal tal.

Abs(Double)

Returnerar det absoluta värdet för ett flyttal med dubbel precision.

Abs(Int16)

Returnerar det absoluta värdet för ett 16-bitars signerat heltal.

Abs(Int32)

Returnerar det absoluta värdet för ett 32-bitars signerat heltal.

Abs(Int64)

Returnerar det absoluta värdet för ett 64-bitars signerat heltal.

Abs(IntPtr)

Returnerar det absoluta värdet för ett inbyggt signerat heltal.

Abs(SByte)

Returnerar det absoluta värdet för ett 8-bitars signerat heltal.

Abs(Single)

Returnerar det absoluta värdet för ett flyttal med enkel precision.

Abs(Decimal)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett Decimal tal.

public:
 static System::Decimal Abs(System::Decimal value);
public static decimal Abs(decimal value);
static member Abs : decimal -> decimal
Public Shared Function Abs (value As Decimal) As Decimal

Parametrar

value
Decimal

Ett tal som är större än eller lika med Decimal.MinValue, men mindre än eller lika med Decimal.MaxValue.

Returer

Ett decimaltal, x, så att 0 ≤ x ≤ Decimal.MaxValue.

Exempel

I följande exempel används Abs(Decimal) metoden för att hämta det absoluta värdet för ett antal Decimal värden.

decimal[] decimals = { Decimal.MaxValue, 12.45M, 0M, -19.69M,
                       Decimal.MinValue };
foreach (decimal value in decimals)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(79228162514264337593543950335) = 79228162514264337593543950335
//       Abs(12.45) = 12.45
//       Abs(0) = 0
//       Abs(-19.69) = 19.69
//       Abs(-79228162514264337593543950335) = 79228162514264337593543950335
open System

let decimals = 
    [ Decimal.MaxValue; 12.45M; 0M
      -19.69M; Decimal.MinValue ]

for value in decimals do
    // The 'abs' function may be used instead.
    printfn $"Abs({value}) = {Math.Abs value}"

// The example displays the following output:
//       Abs(79228162514264337593543950335) = 79228162514264337593543950335
//       Abs(12.45) = 12.45
//       Abs(0) = 0
//       Abs(-19.69) = 19.69
//       Abs(-79228162514264337593543950335) = 79228162514264337593543950335

Kommentarer

Det absoluta värdet för en Decimal är dess numeriska värde utan dess tecken. Det absoluta värdet för både 1.2 och -1.2 är till exempel 1,2.

Gäller för

Abs(Double)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett flyttal med dubbel precision.

public:
 static double Abs(double value);
public static double Abs(double value);
static member Abs : double -> double
Public Shared Function Abs (value As Double) As Double

Parametrar

value
Double

Ett tal som är större än eller lika med Double.MinValue, men mindre än eller lika med Double.MaxValue.

Returer

Ett flyttal med dubbel precision, x, så att 0 ≤ x ≤ Double.MaxValue.

Exempel

I följande exempel används Abs(Double) metoden för att hämta det absoluta värdet för ett antal Double värden.

double[] doubles = { Double.MaxValue, 16.354e-17, 15.098123, 0,
                     -19.069713, -15.058e18, Double.MinValue };
foreach (double value in doubles)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(1.79769313486232E+308) = 1.79769313486232E+308
//       Abs(1.6354E-16) = 1.6354E-16
//       Abs(15.098123) = 15.098123
//       Abs(0) = 0
//       Abs(-19.069713) = 19.069713
//       Abs(-1.5058E+19) = 1.5058E+19
//       Abs(-1.79769313486232E+308) = 1.79769313486232E+308
open System

let doubles = 
    [ Double.MaxValue; 16.354e-17; 15.098123; 0
      -19.069713; -15.058e18; Double.MinValue ]

for value in doubles do
    // The 'abs' function may be used instead.
    printfn $"Abs({value}) = {Math.Abs value}"

// The example displays the following output:
//       Abs(1.79769313486232E+308) = 1.79769313486232E+308
//       Abs(1.6354E-16) = 1.6354E-16
//       Abs(15.098123) = 15.098123
//       Abs(0) = 0
//       Abs(-19.069713) = 19.069713
//       Abs(-1.5058E+19) = 1.5058E+19
//       Abs(-1.79769313486232E+308) = 1.79769313486232E+308
Module Example
   Public Sub Main()
      Dim doubles() As Double = { Double.MaxValue, 16.354e-17, 15.098123, 0, _
                                  -19.069713, -15.058e18, Double.MinValue }
      For Each value As Double In doubles
         Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(1.79769313486232E+308) = 1.79769313486232E+308
'       Abs(1.6354E-16) = 1.6354E-16
'       Abs(15.098123) = 15.098123
'       Abs(0) = 0
'       Abs(-19.069713) = 19.069713
'       Abs(-1.5058E+19) = 1.5058E+19
'       Abs(-1.79769313486232E+308) = 1.79769313486232E+308

Kommentarer

Det absoluta värdet för en Double är dess numeriska värde utan dess tecken. Till exempel är det absoluta värdet för både 1.2e03 och -1.2e03 1.2e03.

Om value är lika med NegativeInfinity eller PositiveInfinityär PositiveInfinityreturvärdet . Om value är lika med NaNär NaNreturvärdet .

Gäller för

Abs(Int16)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett 16-bitars signerat heltal.

public:
 static short Abs(short value);
public static short Abs(short value);
static member Abs : int16 -> int16
Public Shared Function Abs (value As Short) As Short

Parametrar

value
Int16

Ett tal som är större än Int16.MinValue, men mindre än eller lika med Int16.MaxValue.

Returer

Ett 16-bitars signerat heltal, x, så att 0 ≤ x ≤ Int16.MaxValue.

Undantag

value är lika med Int16.MinValue.

Exempel

I följande exempel används Abs(Int16) metoden för att hämta det absoluta värdet för ett antal Int16 värden.

short[] values = { Int16.MaxValue, 10328, 0, -1476, Int16.MinValue };
foreach (short value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(32767) = 32767
//       Abs(10328) = 10328
//       Abs(0) = 0
//       Abs(-1476) = 1476
//       Unable to calculate the absolute value of -32768.
open System

let values = 
    [ Int16.MaxValue; 10328s; 0s; -1476s; Int16.MinValue ]

for value in values do
    try
        // The 'abs' function may be used instead.
        printfn $"Abs({value}) = {Math.Abs value}"
    with :? OverflowException ->
        printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
//       Abs(32767) = 32767
//       Abs(10328) = 10328
//       Abs(0) = 0
//       Abs(-1476) = 1476
//       Unable to calculate the absolute value of -32768.
Module Example
   Public Sub Main()
      Dim values() As Short = { Int16.MaxValue, 10328, 0, -1476, Int16.MinValue }
      For Each value As Short In values
         Try
            Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
         Catch e As OverflowException
            Console.WriteLine("Unable to calculate the absolute value of {0}.", _
                              value)
         End Try   
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(32767) = 32767
'       Abs(10328) = 10328
'       Abs(0) = 0
'       Abs(-1476) = 1476
'       Unable to calculate the absolute value of -32768.

Kommentarer

Det absoluta värdet för en Int16 är dess numeriska värde utan dess tecken. Det absoluta värdet för både 123 och -123 är till exempel 123.

Gäller för

Abs(Int32)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett 32-bitars signerat heltal.

public:
 static int Abs(int value);
public static int Abs(int value);
static member Abs : int -> int
Public Shared Function Abs (value As Integer) As Integer

Parametrar

value
Int32

Ett tal som är större än Int32.MinValue, men mindre än eller lika med Int32.MaxValue.

Returer

Ett 32-bitars signerat heltal, x, så att 0 ≤ x ≤ Int32.MaxValue.

Undantag

Exempel

I följande exempel används Abs(Int32) metoden för att hämta det absoluta värdet för ett antal Int32 värden.

int[] values = { Int32.MaxValue, 16921, 0, -804128, Int32.MinValue };
foreach (int value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(2147483647) = 2147483647
//       Abs(16921) = 16921
//       Abs(0) = 0
//       Abs(-804128) = 804128
//       Unable to calculate the absolute value of -2147483648.
open System

let values = 
    [ Int32.MaxValue; 16921; 0; -804128; Int32.MinValue ]

for value in values do
    try 
        // The 'abs' function may be used instead.
        printfn $"Abs({value}) = {Math.Abs(value)}"
    with :? OverflowException ->
        printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
//       Abs(2147483647) = 2147483647
//       Abs(16921) = 16921
//       Abs(0) = 0
//       Abs(-804128) = 804128
//       Unable to calculate the absolute value of -2147483648.
Module Example
   Public Sub Main()
      Dim values() As Integer = { Int32.MaxValue, 16921, 0, -804128, Int32.MinValue }
      For Each value As Integer In values
         Try
            Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
         Catch e As OverflowException
            Console.WriteLine("Unable to calculate the absolute value of {0}.", _
                              value)
         End Try   
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(2147483647) = 2147483647
'       Abs(16921) = 16921
'       Abs(0) = 0
'       Abs(-804128) = 804128
'       Unable to calculate the absolute value of -2147483648.

Kommentarer

Det absoluta värdet för en Int32 är dess numeriska värde utan dess tecken. Det absoluta värdet för både 123 och -123 är till exempel 123.

Gäller för

Abs(Int64)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett 64-bitars signerat heltal.

public:
 static long Abs(long value);
public static long Abs(long value);
static member Abs : int64 -> int64
Public Shared Function Abs (value As Long) As Long

Parametrar

value
Int64

Ett tal som är större än Int64.MinValue, men mindre än eller lika med Int64.MaxValue.

Returer

Ett 64-bitars signerat heltal, x, så att 0 ≤ x ≤ Int64.MaxValue.

Undantag

value är lika med Int64.MinValue.

Exempel

I följande exempel används Abs(Int64) metoden för att hämta det absoluta värdet för ett antal Int64 värden.

long[] values = { Int64.MaxValue, 109013, 0, -6871982, Int64.MinValue };
foreach (long value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(9223372036854775807) = 9223372036854775807
//       Abs(109013) = 109013
//       Abs(0) = 0
//       Abs(-6871982) = 6871982
//       Unable to calculate the absolute value of -9223372036854775808.
open System

let values = 
    [ Int64.MaxValue; 109013; 0; -6871982; Int64.MinValue ]

for value in values do
    try
        // The 'abs' function may be used instead.
        printfn $"Abs({value}) = {Math.Abs value}"
    with :? OverflowException ->
        printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
//       Abs(9223372036854775807) = 9223372036854775807
//       Abs(109013) = 109013
//       Abs(0) = 0
//       Abs(-6871982) = 6871982
//       Unable to calculate the absolute value of -9223372036854775808.
Module Example
   Public Sub Main()
      Dim values() As Long = { Int64.MaxValue, 109013, 0, -6871982, Int64.MinValue }
      For Each value As Long In values
         Try
            Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
         Catch e As OverflowException
            Console.WriteLine("Unable to calculate the absolute value of {0}.", _
                              value)
         End Try   
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(9223372036854775807) = 9223372036854775807
'       Abs(109013) = 109013
'       Abs(0) = 0
'       Abs(-6871982) = 6871982
'       Unable to calculate the absolute value of -9223372036854775808.

Kommentarer

Det absoluta värdet för en Int64 är dess numeriska värde utan dess tecken. Det absoluta värdet för både 123 och -123 är till exempel 123.

Gäller för

Abs(IntPtr)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett inbyggt signerat heltal.

public:
 static IntPtr Abs(IntPtr value);
public static IntPtr Abs(IntPtr value);
public static nint Abs(nint value);
static member Abs : nativeint -> nativeint
Public Shared Function Abs (value As IntPtr) As IntPtr

Parametrar

value
IntPtr

nint

nativeint

Ett tal som är större än MinValue, men mindre än eller lika med MaxValue.

Returer

IntPtr

nativeint

Ett inbyggt signerat heltal, x, så att 0 ≤ x ≤ MaxValue.

Gäller för

Abs(SByte)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Viktigt!

Detta API uppfyller inte CLS.

Returnerar det absoluta värdet för ett 8-bitars signerat heltal.

public:
 static System::SByte Abs(System::SByte value);
[System.CLSCompliant(false)]
public static sbyte Abs(sbyte value);
[<System.CLSCompliant(false)>]
static member Abs : sbyte -> sbyte
Public Shared Function Abs (value As SByte) As SByte

Parametrar

value
SByte

Ett tal som är större än SByte.MinValue, men mindre än eller lika med SByte.MaxValue.

Returer

Ett 8-bitars signerat heltal, x, så att 0 ≤ x ≤ SByte.MaxValue.

Attribut

Undantag

Exempel

I följande exempel används Abs(SByte) metoden för att hämta det absoluta värdet för ett antal SByte värden.

sbyte[] values = { SByte.MaxValue, 98, 0, -32, SByte.MinValue };
foreach (sbyte value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(127) = 127
//       Abs(98) = 98
//       Abs(0) = 0
//       Abs(-32) = 32
//       Unable to calculate the absolute value of -128.
open System

let values = 
    [ SByte.MaxValue; 98y; 0y; -32y; SByte.MinValue ]

for value in values do
    try
        // The 'abs' function may be used instead.
        printfn $"Abs({value}) = {Math.Abs value}"
    with :? OverflowException ->
        printfn $"Unable to calculate the absolute value of {value}."

// The example displays the following output:
//       Abs(127) = 127
//       Abs(98) = 98
//       Abs(0) = 0
//       Abs(-32) = 32
//       Unable to calculate the absolute value of -128.
Module Example
   Public Sub Main()
      Dim values() As SByte = { SByte.MaxValue, 98, 0, -32, SByte.MinValue }
      For Each value As SByte In values
         Try
            Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
         Catch e As OverflowException
            Console.WriteLine("Unable to calculate the absolute value of {0}.", _
                              value)
         End Try   
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(127) = 127
'       Abs(98) = 98
'       Abs(0) = 0
'       Abs(-32) = 32
'       Unable to calculate the absolute value of -128.

Kommentarer

Det absoluta värdet för en signerad byte är dess numeriska värde utan dess tecken. Det absoluta värdet för både 12 och -12 är till exempel 12.

Gäller för

Abs(Single)

Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs
Källa:
Math.cs

Returnerar det absoluta värdet för ett flyttal med enkel precision.

public:
 static float Abs(float value);
public static float Abs(float value);
static member Abs : single -> single
Public Shared Function Abs (value As Single) As Single

Parametrar

value
Single

Ett tal som är större än eller lika med Single.MinValue, men mindre än eller lika med Single.MaxValue.

Returer

Ett flyttal med enkel precision, x, så att 0 ≤ x ≤ Single.MaxValue.

Exempel

I följande exempel används Abs(Single) metoden för att hämta det absoluta värdet för ett antal Single värden.

float[] values= { Single.MaxValue, 16.354e-12F, 15.098123F, 0F,
                  -19.069713F, -15.058e17F, Single.MinValue };
foreach (float value in values)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(3.402823E+38) = 3.402823E+38
//       Abs(1.6354E-11) = 1.6354E-11
//       Abs(15.09812) = 15.09812
//       Abs(0) = 0
//       Abs(-19.06971) = 19.06971
//       Abs(-1.5058E+18) = 1.5058E+18
//       Abs(-3.402823E+38) = 3.402823E+38
open System

let values = 
    [ Single.MaxValue; 16.354e-12f; 15.098123f; 0f
      -19.069713f; -15.058e17f; Single.MinValue ]

for value in values do
    // The 'abs' function may be used instead.
    printfn $"Abs({value}) = {Math.Abs value}"

// The example displays the following output:
//       Abs(3.402823E+38) = 3.402823E+38
//       Abs(1.6354E-11) = 1.6354E-11
//       Abs(15.09812) = 15.09812
//       Abs(0) = 0
//       Abs(-19.06971) = 19.06971
//       Abs(-1.5058E+18) = 1.5058E+18
//       Abs(-3.402823E+38) = 3.402823E+38
Module Example
   Public Sub Main()
      Dim values() As Single = { Single.MaxValue, 16.354e-12, 15.098123, 0, _
                                  -19.069713, -15.058e17, Single.MinValue }
      For Each value As Single In values
         Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value))
      Next
   End Sub
End Module
' The example displays the following output:
'       Abs(3.402823E+38) = 3.402823E+38
'       Abs(1.6354E-11) = 1.6354E-11
'       Abs(15.09812) = 15.09812
'       Abs(0) = 0
'       Abs(-19.06971) = 19.06971
'       Abs(-1.5058E+18) = 1.5058E+18
'       Abs(-3.402823E+38) = 3.402823E+38

Kommentarer

Det absoluta värdet för en Single är dess numeriska värde utan dess tecken. Det absoluta värdet för både 1.2e-03 och -1.2e03 är till exempel 1.2e03.

Om value är lika med NegativeInfinity eller PositiveInfinityär PositiveInfinityreturvärdet . Om value är lika med NaNär NaNreturvärdet .

Gäller för