C#.Net数学类及其方法
Math是C#.Net中的预定义类,它具有许多无需使用运算符即可执行数学运算的方法。
这篇文章包含一些常见和最受欢迎的Math类的方法,这些方法可以帮助您在C#程序中执行相关的操作。
以下是c#中使用的以下重要方法:
Math.Pow()
Math.Sqrt()
Math.Max()
Math.Min()
Math.Ceiling()
Math.Floor()
1)Math.Pow()
此方法用于计算给定数字的幂。
2)Math.Sqrt()
此方法用于计算给定数的平方根。
3)Math.Max()
此方法用于从两个给定数中查找最大数。
4)Math.Min()
此方法用于从两个给定数中查找最小数。
5)Math.Ceiling()
此方法用于吊起给定的浮点数。
6)Math.Floor()
此方法用于给定浮点数的底数。
看程序:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double num = 0;
double power = 0;
double result = 0;
Console.Write("Enter Number : ");
num = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Power : ");
power = Convert.ToDouble(Console.ReadLine());
result = Math.Pow(num, power);
Console.WriteLine("Result : " + result);
result = Math.Sqrt(16);
Console.WriteLine("Result : " + result);
result = Math.Max(10.2, 10.5);
Console.WriteLine("Result : " + result);
result = Math.Min(10.2, 10.5);
Console.WriteLine("Result : " + result);
result = Math.Ceiling(10.2);
Console.WriteLine("Result : " + result);
result = Math.Floor(10.2);
Console.WriteLine("Result : " + result);
}
}
}输出结果
Enter Number : 2 Enter Power : 3 Result : 8 Result : 4 Result : 10.5 Result : 10.2 Result : 11 Result : 10