【ASP.NET】 中 system.math 函数使用

 今天在做项目的过程中要进行很多的运算, 这个时候想起了math 函数。里面封装了很多常用的函数,就在网上收集了一些用法,记录下来,以备不时只需。

View Code
  1 /* 字段 */
  2 Math.E;             //2.71828182845905
  3 Math.PI;            //3.14159265358979
  4 
  5 /* 静态方法 */
  6 Math.Abs;           //绝对值
  7 Math.Acos;          //反余弦
  8 Math.Asin;          //反正弦
  9 Math.Atan;          //反正切
 10 Math.Atan2;         //反正切, 两参数
 11 Math.BigMul;        //int32 * int32 = int64
 12 Math.Ceiling;       //取 >= 的最小整数
 13 Math.Cos;           //余弦
 14 Math.Cosh;          //双曲余弦
 15 Math.DivRem;        //取商和余数
 16 Math.Exp;           //求 e 的指定次幂
 17 Math.Floor;         //取 <= 的最大整数
 18 Math.IEEERemainder; //求余
 19 Math.Log;           //自然对数
 20 Math.Log10;         //以 10 为底的对数
 21 Math.Max;           //取大
 22 Math.Min;           //取小
 23 Math.Pow;           //求幂
 24 Math.Round;         //就近舍入, 可指定精度
 25 Math.Sign;          //取符号, 或返回 -1、0、1 
 26 Math.Sin;           //正弦
 27 Math.Sinh;          //双曲正弦
 28 Math.Sqrt;          //平方根
 29 Math.Tan;           //正切
 30 Math.Tanh;          //双曲正切
 31 Math.Truncate;      //取整
 32 
 33 
 34 
 35 
 36 练习:
 37 --------------------------------------------------------------------------------
 38  
 39 //Truncate()、Floor()、Ceiling()
 40 protected void Button1_Click(object sender, EventArgs e)
 41 {
 42     double n1 = Math.Truncate(Math.PI); // 3
 43 
 44     double n2 = Math.Floor(2.5);        // 2
 45     double n3 = Math.Floor(-2.5);       //-3
 46 
 47     double n4 = Math.Ceiling(2.5);      // 3
 48     double n5 = Math.Ceiling(-2.5);     //-2
 49 
 50     TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5);
 51 }
 52 
 53 //就近舍入(取偶)
 54 protected void Button2_Click(object sender, EventArgs e)
 55 {
 56     double n1 = Math.Round(0.5);  // 0
 57     double n2 = Math.Round(1.5);  // 2
 58     double n3 = Math.Round(2.5);  // 2
 59     double n4 = Math.Round(3.5);  // 4
 60     double n5 = Math.Round(-0.5); // 0
 61     double n6 = Math.Round(-1.5); //-2
 62     double n7 = Math.Round(-2.5); //-2
 63     double n8 = Math.Round(-3.5); //-4
 64 
 65     TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6, "\n", n7, "\n", n8);
 66 }
 67 
 68 //四舍五入
 69 protected void Button3_Click(object sender, EventArgs e)
 70 {
 71     double n1 = Math.Round(0.5, MidpointRounding.AwayFromZero);  // 1
 72     double n2 = Math.Round(1.5, MidpointRounding.AwayFromZero);  // 2 
 73     double n3 = Math.Round(2.5, MidpointRounding.AwayFromZero);  // 3
 74     double n4 = Math.Round(3.5, MidpointRounding.AwayFromZero);  // 4 
 75     double n5 = Math.Round(-0.5, MidpointRounding.AwayFromZero); //-1
 76     double n6 = Math.Round(-1.5, MidpointRounding.AwayFromZero); //-2
 77     double n7 = Math.Round(-2.5, MidpointRounding.AwayFromZero); //-3
 78     double n8 = Math.Round(-3.5, MidpointRounding.AwayFromZero); //-4
 79 
 80     TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6, "\n", n7, "\n", n8);
 81 }
 82 
 83 //指定小数位数(0..28)的舍入
 84 protected void Button4_Click(object sender, EventArgs e)
 85 {
 86     double n1 = Math.Round(3.126, 2);  // 3.13
 87     double n2 = Math.Round(3.124, 2);  // 3.12
 88 
 89     double n3 = Math.Round(3.125, 2);  // 3.12
 90     double n4 = Math.Round(3.135, 2);  // 3.14
 91 
 92     double n5 = Math.Round(3.125, 2, MidpointRounding.AwayFromZero);  // 3.13
 93     double n6 = Math.Round(3.135, 2, MidpointRounding.AwayFromZero);  // 3.14
 94 
 95     TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6);
 96 }
 97 
 98 
 99 
100 asp.net 系统中math数学函数  能够很快的帮我们进行运算!

希望分享的东西对你也有帮助。

 

posted @ 2012-06-19 15:14  Ruicky  阅读(704)  评论(0编辑  收藏  举报