在C#中, Abs()是Math类方法,用于返回指定数字的绝对值。通过将不同类型的参数传递给此方法,可以使其过载。
- Math.Abs(十进制)
- Math.Abs(Double)
- Math.Abs(Int16)
- Math.Abs(Int32)
- Math.Abs(Int64)
- Math.Abs(SByte)
- Math.Abs(Single)
Math.Abs(十进制)
此方法用于返回十进制数的绝对值。
句法:
public static decimal Abs (decimal val);
范围:
val: It is the required number which is greater than or equal to Decimal.MinValue, but less than or equal to Decimal.MaxValue of type System.Decimal.
返回类型:返回一个十进制数字,例如r ,即0≤r≤Decimal.MaxValue 。
例子:
// C# Program to illlustrate the // Math.Abs(Decimal) Method using System; class Geeks { // Main Method public static void Main() { // Taking decimal values decimal[] deci = {Decimal.MinValue, 45.14M, 0M, -17.47M, Decimal.MaxValue}; // using foreach loop foreach(decimal value in deci) // Displaying the result Console.WriteLine("Absolute value of {0} = {1}", value, Math.Abs(value)); } }
输出:
Absolute value of -79228162514264337593543950335 = 79228162514264337593543950335 Absolute value of 45.14 = 45.14 Absolute value of 0 = 0 Absolute value of -17.47 = 17.47 Absolute value of 79228162514264337593543950335 = 79228162514264337593543950335
Math.Abs(Double)
此方法用于返回双精度浮点数的绝对值。
句法:
public static double Abs (double val);
范围:
val: It is the required number which is greater than or equal to Double.MinValue, but less than or equal to Double.MaxValue of type System.Double.
返回类型:返回一个双精度浮点数,例如r ,使得0≤r≤Double.MaxValue 。
笔记:
- 如果val等于NegativeInfinity或PositiveInfinity ,则返回值将为PositiveInfinity 。
- 如果val等于NaN,则返回值为NaN 。
例子:
// C# Program to illlustrate the // Math.Abs(Double) Method using System; class Geeks { // Main Method public static void Main() { // Taking a NaN Double nan = Double.NaN; // Taking double values double[] doub = {Double.MinValue, 27.58, 0.0, 56.48e10, nan, Double.MaxValue}; // using foreach loop foreach(double value in doub) // Displaying the result Console.WriteLine("Absolute value of {0} = {1}", value, Math.Abs(value)); } }
输出:
Absolute value of -1.79769313486232E+308 = 1.79769313486232E+308 Absolute value of 27.58 = 27.58 Absolute value of 0 = 0 Absolute value of 564800000000 = 564800000000 Absolute value of NaN = NaN Absolute value of 1.79769313486232E+308 = 1.79769313486232E+308
Math.Abs(Int16)
此方法用于返回16位带符号整数的绝对值。
句法:
public static short Abs (short val);
范围:
val: It is the required number which is greater than Int16.MinValue, but less than or equal to Int16.MaxValue of type System.Int16.
返回类型:返回16位有符号整数,说r ,使得0≤r≤Int16.MaxValue 。
异常:如果val的值等于Int16.MinValue,则此方法将给出OverflowException 。
例子:
// C# Program to illlustrate the // Math.Abs(Int16) Method using System; class Geeks { // Main Method public static void Main() { // Taking short values short[] sh = {Int16.MaxValue, 1482, -142, 0 }; // using foreach loop foreach(short value in sh) // Displaying the result Console.WriteLine("Absolute value of {0} = {1}", value, Math.Abs(value)); } }
输出:
Absolute value of 32767 = 32767 Absolute value of 1482 = 1482 Absolute value of -142 = 142 Absolute value of 0 = 0
Math.Abs(Int32)
此方法用于返回32位有符号整数的绝对值。
句法:
public static int Abs (int val);
范围:
val: It is the required number which is greater than Int32.MinValue, but less than or equal to Int32.MaxValue of type System.Int32.
返回类型:返回32位有符号整数,说r ,使得0≤r≤Int32.MaxValue 。
异常:如果val的值等于Int32.MinValue,则此方法将给出OverflowException 。
例子:
// C# Program to illlustrate the // Math.Abs(Int32) Method using System; class Geeks { // Main Method public static void Main() { // Taking int values int[] int_val = {Int32.MaxValue, 13482, -65525, 0}; // using foreach loop foreach(int value in int_val) // Displaying the result Console.WriteLine("Absolute value of {0} = {1}", value, Math.Abs(value)); } }
输出:
Absolute value of 2147483647 = 2147483647 Absolute value of 13482 = 13482 Absolute value of -65525 = 65525 Absolute value of 0 = 0
重载列表中共有7种方法。在这里,我们将仅讨论前4种方法,其余3种方法将在C#中讨论。 Math.Abs()方法|设置– 2 。
参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.math.abs?view=netframework-4.7.2