Math类的认识与使用
- 程序集:由.net编译生成的exe文件或者dll文件
- Math类的程序集:n 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\mscorlib.dll - 这个Math类的命名空间是:System.Window
- Math类是静态类
- 有两个常用属性:
// // 摘要: // 表示圆的周长与其直径的比值,由常数 π 指定。 public const double PI = 3.1415926535897931; // // 摘要: // 表示自然对数的底,它由常数 e 指定。 public const double E = 2.7182818284590451;
- Math类常用的一些方法;
- public static decimal Abs(类型,值); 数字的绝对值
- public static double Acos(double d); 反余弦值为指定数字的角度 d:表示余弦值的数字,其中d必须大于或等于-1 但小于或等于1.
- public static double Asin(double d);反正弦=返回正弦值为指定数字的角度。d:表示正弦值的数字,其中 d 必须大于或等于 -1 但小于或等于 1。
- public static double Atan(double d);返回正切值为指定数字的角度。
- public static double Atan2(double y, double x);返回正切值为两个指定数字的商的角度。y:点的y坐标 x:点的x坐标
- public static long BigMul(int a, int b); 两个数的乘积 参数:a:第一个数 b:第二个数 返回结果包含指定数字乘积的数
- public static decimal Ceiling(decimal d); 返回大于或等于指定的十进制的最小整数的值
- public static double Cos(double d);返回指定角度的余弦值
- public static double Cosh(double value);返回指定角度的双曲线余弦值余弦值 value:以弧度计量的角度
- public static int DivRem(int a, int b, out int result);计算整数的商,并通过输出参数返回余数,a:被除数 b:除数 result:余数 这里的int类型可以换成long类型
- public static double Exp(double d); 返回e的指定次幂。 d:指定幂的数字
- public static double IEEERemainder(double x, double y); 返回指定数字被另一指定数字相除的余数 x:被除数 y:除数
- public static int Max(int val1, int val2); 返回两个整数类型两个较大的一个
- public static int Min(int val1, int val2);返回两个整数类型两个较小的一个
- public static double Pow(double x, double y); 返回指定数字的指定次幂 返回:数字x的y次幂
- public static decimal Round(decimal d, int decimals); 将小数值按指定的小数位数舍入 d:要舍入的小数 decimals:返回值的小数位数 返回结果:接近d的decimal位小数数字
- public static int Sign(int value); 返回带有符号整数的整数
实例
前台代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBox Name="test1" Width="300" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="请输入圆的半径"
Foreground="MediumPurple" Background="Lavender" FontSize="13" FontFamily="宋体" FontWeight="Light"></TextBox>
<Button Style="{StaticResource ButtonInfo}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="周长" Margin="20,45,20,20" Click="Button_Click"> </Button>
<Button Style="{StaticResource ButtonWarning}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="面积" Margin="20,45,20,20" Click="Button_Click_1"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Name="testBox1" Width="150" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="请输入第一条边的长度"
Foreground="MediumPurple" Background="LightPink" FontSize="13" FontFamily="宋体" FontWeight="Light"></TextBox>
<TextBox Name="testBox2" Width="150" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="请输入第二条边的长度"
Foreground="MediumPurple" Background="LightBlue" FontSize="13" FontFamily="宋体" FontWeight="Light" Margin="20,0,0,0"></TextBox>
<TextBox Name="testBox3" Width="150" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="第三条边的长度"
Foreground="MediumPurple" Background="LightCyan" FontSize="13" FontFamily="宋体" FontWeight="Light" Margin="20,0,0,0"></TextBox>
<Button Style="{StaticResource ButtonSuccess}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="求另一边" Margin="20,45,20,20" Click="Button_Click_2" ></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2">
<TextBox Name="testBox4" Width="150" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="请输入需计算的角度"
Foreground="MediumPurple" Background="LightCoral" FontSize="13" FontFamily="宋体" FontWeight="Light"></TextBox>
<TextBox Name="testBox5" Width="150" Height="60" Style="{StaticResource TextBoxExtend}" hc:InfoElement.Title="结果"
Foreground="MediumPurple" Background="LightGray" FontSize="13" FontFamily="宋体" FontWeight="Light" Margin="20,0,0,0"></TextBox>
<Button Style="{StaticResource ButtonDanger}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="Sin(正弦)" Margin="20,45,20,20" Click="Button_Click_3" ></Button>
<Button Style="{StaticResource ButtonWarning}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="Cos(余弦)" Margin="20,45,20,20" Click="Button_Click_4" ></Button>
<Button Style="{StaticResource ButtonInfo}" Height="32" Width="90" FontFamily="宋体" FontSize="13"
Content="Tan(正切)" Margin="20,45,20,20" Click="Button_Click_5" ></Button>
</StackPanel>
</Grid>
后台代码
private void Button_Click(object sender, RoutedEventArgs e) {
double r = Convert.ToDouble(test1.Text);
double pi = 3.14;
double perimeter = 2 * pi * r;
MessageBox.Show(string.Format("这个圆的半径是{0},周长是{1}",r,perimeter));
}
private void Button_Click_1(object sender, RoutedEventArgs e) {
double r = Convert.ToDouble(test1.Text);
double pi = 3.14;
double area = pi * r * r;
MessageBox.Show(string.Format("这个圆的半径是{0},面积是{1}", r, area));
}
private void Button_Click_2(object sender, RoutedEventArgs e) {
try {
double one = Convert.ToDouble(testBox1.Text);
double two = Convert.ToDouble(testBox2.Text);
double three = Math.Sqrt(one * one + two * two);
testBox3.Text = Convert.ToString(three);
}
catch {
testBox3.Text = Convert.ToString("输入错误请重新输入");
}
}
private void Button_Click_3(object sender, RoutedEventArgs e) {
double degree = Convert.ToDouble(testBox4.Text);
testBox5.Text = Math.Sin(degree).ToString();
}
private void Button_Click_4(object sender, RoutedEventArgs e) {
double degree = Convert.ToDouble(testBox4.Text);
testBox5.Text = Math.Cos(degree).ToString();
}
private void Button_Click_5(object sender, RoutedEventArgs e) {
double degree = Convert.ToDouble(testBox4.Text);
testBox5.Text = Math.Tan(degree).ToString();
}