c# 取整

原创
2016/10/12 00:27
阅读数 1.1K
用到一个数据 类 math ,调用他的方法
 
1: int类型的相除得到的结果是 int类型的整数 int num = 9/5;【num=1 】  double   num = 9/5;【num=1.0】
2: double.Parse(string str)
4 double num=celing(4.3)【num=4】
ok;
 
向上取整

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)); 
C#取整函数的相关内容就向你介绍到这里,希望对你了解和学习C#取整函数有所帮助。 
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部