随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


成员:
/* 字段 */
Math.E;             //2.71828182845905
Math.PI;            //3.14159265358979

/* 静态方法 */
Math.Abs;           //绝对值
Math.Acos;          //反余弦
Math.Asin;          //反正弦
Math.Atan;          //反正切
Math.Atan2;         //反正切, 两参数
Math.BigMul;        //int32 * int32 = int64
Math.Ceiling;       //取 >= 的最小整数
Math.Cos;           //余弦
Math.Cosh;          //双曲余弦
Math.DivRem;        //取商和余数
Math.Exp;           //求 e 的指定次幂
Math.Floor;         //取 <= 的最大整数
Math.IEEERemainder; //求余
Math.Log;           //自然对数
Math.Log10;         //以 10 为底的对数
Math.Max;           //取大
Math.Min;           //取小
Math.Pow;           //求幂
Math.Round;         //就近舍入, 可指定精度
Math.Sign;          //取符号, 或返回 -1、0、1 
Math.Sin;           //正弦
Math.Sinh;          //双曲正弦
Math.Sqrt;          //平方根
Math.Tan;           //正切
Math.Tanh;          //双曲正切
Math.Truncate;      //取整


练习:
//Truncate()、Floor()、Ceiling()
protected void Button1_Click(object sender, EventArgs e)
{
    double n1 = Math.Truncate(Math.PI); // 3

    double n2 = Math.Floor(2.5);        // 2
    double n3 = Math.Floor(-2.5);       //-3

    double n4 = Math.Ceiling(2.5);      // 3
    double n5 = Math.Ceiling(-2.5);     //-2

    TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5);
}

//就近舍入(取偶)
protected void Button2_Click(object sender, EventArgs e)
{
    double n1 = Math.Round(0.5);  // 0
    double n2 = Math.Round(1.5);  // 2
    double n3 = Math.Round(2.5);  // 2
    double n4 = Math.Round(3.5);  // 4
    double n5 = Math.Round(-0.5); // 0
    double n6 = Math.Round(-1.5); //-2
    double n7 = Math.Round(-2.5); //-2
    double n8 = Math.Round(-3.5); //-4

    TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6, "\n", n7, "\n", n8);
}

//四舍五入
protected void Button3_Click(object sender, EventArgs e)
{
    double n1 = Math.Round(0.5, MidpointRounding.AwayFromZero);  // 1
    double n2 = Math.Round(1.5, MidpointRounding.AwayFromZero);  // 2 
    double n3 = Math.Round(2.5, MidpointRounding.AwayFromZero);  // 3
    double n4 = Math.Round(3.5, MidpointRounding.AwayFromZero);  // 4 
    double n5 = Math.Round(-0.5, MidpointRounding.AwayFromZero); //-1
    double n6 = Math.Round(-1.5, MidpointRounding.AwayFromZero); //-2
    double n7 = Math.Round(-2.5, MidpointRounding.AwayFromZero); //-3
    double n8 = Math.Round(-3.5, MidpointRounding.AwayFromZero); //-4

    TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6, "\n", n7, "\n", n8);
}

//指定小数位数(0..28)的舍入
protected void Button4_Click(object sender, EventArgs e)
{
    double n1 = Math.Round(3.126, 2);  // 3.13
    double n2 = Math.Round(3.124, 2);  // 3.12

    double n3 = Math.Round(3.125, 2);  // 3.12
    double n4 = Math.Round(3.135, 2);  // 3.14

    double n5 = Math.Round(3.125, 2, MidpointRounding.AwayFromZero);  // 3.13
    double n6 = Math.Round(3.135, 2, MidpointRounding.AwayFromZero);  // 3.14

    TextBox1.Text = string.Concat(n1, "\n", n2, "\n", n3, "\n", n4, "\n", n5, "\n", n6);
}

posted on   万一  阅读(1616)  评论(2编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2010-01-03 GdiPlus[60]: 图像(十二) IGPImageAttributes 的更多方法
2009-01-03 C# 语法练习(15): 接口
2009-01-03 C# 语法练习(14): 类[六] - 事件
2009-01-03 如何在 "万一的 Delphi 博客" 回复自动格式化的着色代码?
2009-01-03 C# 语法练习(13): 类[五] - 索引器
2008-01-03 Delphi 中的 XMLDocument 类详解(14) - 遍历 XML 文件
2008-01-03 Delphi 中的 XMLDocument 类详解(13) - 关于 XML 属性


点击右上角即可分享
微信分享提示