Math.Abs 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳指定數字的絕對值。
多載
| 名稱 | Description |
|---|---|
| Abs(Decimal) |
回傳一個 Decimal 數字的絕對值。 |
| Abs(Double) |
回傳一個雙精度浮點數的絕對值。 |
| Abs(Int16) |
回傳一個 16 位元有符號整數的絕對值。 |
| Abs(Int32) |
回傳一個 32 位元有符號整數的絕對值。 |
| Abs(Int64) |
回傳一個 64 位元有符號整數的絕對值。 |
| Abs(IntPtr) |
回傳一個本地有符號整數的絕對值。 |
| Abs(SByte) |
回傳一個 8 位元有符號整數的絕對值。 |
| Abs(Single) |
回傳一個單精度浮點數的絕對值。 |
Abs(Decimal)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個 Decimal 數字的絕對值。
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
參數
傳回
一個十進位數字 x,使得 0 ≤ x ≤ 十進位。最大值。
範例
以下範例使用該 Abs(Decimal) 方法來取得多個 Decimal 值的絕對值。
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
備註
a Decimal 的絕對值是其數值(不含符號)。 例如,1.2 和 -1.2 的絕對值都是 1.2。
適用於
Abs(Double)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個雙精度浮點數的絕對值。
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
參數
- value
- Double
一個大於或等於 Double.MinValue 但小於或等於 Double.MaxValue 的數字。
傳回
一個雙精度浮點數 x,使得 0 ≤ x ≤ Double.MaxValue。
範例
以下範例使用該 Abs(Double) 方法來取得多個 Double 值的絕對值。
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
備註
a Double 的絕對值是其數值(不含符號)。 例如,1.2e03 和 -1.2e03 的絕對值都是 1.2e03。
若 value 等於 或 NegativeInfinity,則返回值為 PositiveInfinityPositiveInfinity 。 若 value 等於 NaN,則返回值為 NaN。
適用於
Abs(Int16)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個 16 位元有符號整數的絕對值。
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
參數
- value
- Int16
一個大於 Int16.MinValue 但小於或等於 Int16.MaxValue 的數字。
傳回
一個 16 位元有號整數 x,使得 0 ≤ x ≤ Int16.MaxValue。
例外狀況
value 等於 Int16.MinValue。
範例
以下範例使用該 Abs(Int16) 方法來取得多個 Int16 值的絕對值。
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.
備註
一個 Int16 的絕對值是其數值,不含符號。 例如,123 和 -123 的絕對值都是 123。
適用於
Abs(Int32)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個 32 位元有符號整數的絕對值。
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
參數
- value
- Int32
一個大於 Int32.MinValue 但小於或等於 Int32.MaxValue 的數字。
傳回
一個 32 位元有符號整數 x,使得 0 ≤ x ≤ Int32.MaxValue。
例外狀況
value 等於 Int32.MinValue。
範例
以下範例使用該 Abs(Int32) 方法來取得多個 Int32 值的絕對值。
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.
備註
一個 Int32 的絕對值是其數值,不含符號。 例如,123 和 -123 的絕對值都是 123。
適用於
Abs(Int64)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個 64 位元有符號整數的絕對值。
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
參數
- value
- Int64
一個大於 Int64.MinValue 但小於或等於 Int64.MaxValue 的數字。
傳回
一個 64 位元有符號整數 x,使得 0 ≤ x ≤ Int64.MaxValue。
例外狀況
value 等於 Int64.MinValue。
範例
以下範例使用該 Abs(Int64) 方法來取得多個 Int64 值的絕對值。
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.
備註
一個 Int64 的絕對值是其數值,不含符號。 例如,123 和 -123 的絕對值都是 123。
適用於
Abs(IntPtr)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個本地有符號整數的絕對值。
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
參數
傳回
nativeint
一個帶符號整數 x,使得 0 ≤ x ≤ MaxValue。
適用於
Abs(SByte)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
重要
此 API 不符合 CLS 規範。
回傳一個 8 位元有符號整數的絕對值。
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
參數
- value
- SByte
一個大於 SByte.MinValue 但小於或等於 SByte.MaxValue 的數字。
傳回
一個 8 位元有符號整數 x,使得 0 ≤ x ≤ SByte.MaxValue。
- 屬性
例外狀況
value 等於 SByte.MinValue。
範例
以下範例使用該 Abs(SByte) 方法來取得多個 SByte 值的絕對值。
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.
備註
有符號位元組的絕對值為其數值,但不含符號。 例如,12 和 -12 的絕對值都是 12。
適用於
Abs(Single)
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
- 來源:
- Math.cs
回傳一個單精度浮點數的絕對值。
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
參數
- value
- Single
一個大於或等於 Single.MinValue 但小於或等於 Single.MaxValue 的數字。
傳回
一個單精度浮點數 x,使得 0 ≤ x ≤ Single.MaxValue。
範例
以下範例使用該 Abs(Single) 方法來取得多個 Single 值的絕對值。
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
備註
a Single 的絕對值是其數值(不含符號)。 例如,1.2e-03 和 -1.2e03 的絕對值都是 1.2e03。
若 value 等於 或 NegativeInfinity,則返回值為 PositiveInfinityPositiveInfinity 。 若 value 等於 NaN,則返回值為 NaN。