若要在C#中查找数字的绝对值,请使用Math.Abs方法。
首先设置数字-
int val1 = 77; int val2 = -88;
现在获取两个新变量,并获得上述两个数字的绝对值-
int abs1 = Math.Abs(val1); int abs2 = Math.Abs(val2);
让我们看完整的代码以显示数字的绝对值-
using System; class Program { static void Main() { int val1 = 77; int val2 = -88; Console.WriteLine("Before..."); Console.WriteLine(val1); Console.WriteLine(val2); int abs1 = Math.Abs(val1); int abs2 = Math.Abs(val2); Console.WriteLine("After..."); Console.WriteLine(abs1); Console.WriteLine(abs2); } }
输出结果
Before... 77 -88 After... 77 88