luchunqing
向上取整
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#取整函数有所帮助。

分类:

技术点:

相关文章:

  • 2021-10-12
  • 2021-09-25
猜你喜欢
  • 2021-08-18
  • 2022-03-03
  • 2021-12-27
  • 2021-10-09
  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案