C 取整和四舍五入

C 取整和四舍五入doubleAJS Math Round double Parse memberSum Agrade divideBy 0 5 ToString 0 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp doubleABJS Math Round double Parse memberSum Agrade Bgrade divideBy

向上取整
math.ceiling(1) = 1
math.ceiling(1.1) = 2
math.ceiling(1.5) = 2
向下取整
math.float(1) = 1
math.float(1.1) = 1
math.float(1.5) = 1

C#取整函数实例应用详解

C#取整函数的相关使用是我们在实际开发应用中经常会碰到的具体的实用性概念,那么如何使用好C#取整函数呢?首先我们要明白什么是C#取整函数以及C#取整函数的使用规范。
C#取整函数使用实例:
Math.Round是”就近舍入”,当要舍入的是5时与”四舍五入”不同(取偶数),如:
Math.Round(0.5,0)=0    
Math.Round(1.5,0)=2    
Math.Round(2.5,0)=2    
Math.Round(3.5,0)=4    
….    
Math.Ceiling(3.1)=4;    
Math.Floor(3.9)=3;   
取天板值与地板值,与”四舍五入”无关。其实Floor的结果与(int)相同,因此也可以这样写Math.Floor((double)2/3+0.5)
floor 和 ceil是math unit 里的函数,使用前要先 Uses Math。
trunc 和 round 是system unit 里的函数,缺省就可以用。
floor 直接往小的取,比如 floor(-123.55)=-124,floor(123.55)=123
trunc 直接切下整数,比如 trunc(-123.55)=-123, floor(123.55)=123
ceil 直接往大的取,比如 ceil(-123.55)=-123, ceil(123.55)=124
round 计算四舍五入,比如 round(-123.55)=-124,round(123.55)=124
C#取整函数向上取整实例
int a = 5; int b = 2;   lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b));

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/225783.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 上午8:33
下一篇 2026年3月17日 上午8:33


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号