C# 之 Math取整
生活随笔
收集整理的這篇文章主要介紹了
C# 之 Math取整
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主要用到 System 命名空間下的一個數據類 Math ,調用他的方法。
C#取整函數使用詳解: 1、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
2、Math.Truncate 計算雙精度浮點數的整數部分,即直接取整數,如:
Math.Truncate(-123.55)=-123,
Math.Truncate(123.55)=123 3、Math.Ceiling 取天板值,即向上取整,與"四舍五入"無關。
Math.Ceiling(1) = 1
Math.Ceiling(1.1) = 2
Math.Ceiling(1.5) = 2
Math.Ceiling(3.1) = 4
4、Math.Floor 取地板值,即向下取整,與"四舍五入"無關。
Math.Floor(1) = 1
Math.Floor(1.1) = 1
Math.Floor(1.5) = 1 Math.Floor(3.9) = 3
取天板值與地板值,與"四舍五入"無關。其實Floor的結果與(int)相同,因此也可以這樣寫Math.Floor((double)2/3+0.5) int a = 5; int b = 2; lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b));
C#取整函數使用詳解: 1、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
2、Math.Truncate 計算雙精度浮點數的整數部分,即直接取整數,如:
Math.Truncate(-123.55)=-123,
Math.Truncate(123.55)=123 3、Math.Ceiling 取天板值,即向上取整,與"四舍五入"無關。
Math.Ceiling(1) = 1
Math.Ceiling(1.1) = 2
Math.Ceiling(1.5) = 2
Math.Ceiling(3.1) = 4
4、Math.Floor 取地板值,即向下取整,與"四舍五入"無關。
Math.Floor(1) = 1
Math.Floor(1.1) = 1
Math.Floor(1.5) = 1 Math.Floor(3.9) = 3
取天板值與地板值,與"四舍五入"無關。其實Floor的結果與(int)相同,因此也可以這樣寫Math.Floor((double)2/3+0.5) int a = 5; int b = 2; lbl.Text = Convert.ToString(Math.Ceiling((double)a / (double)b));
轉載于:https://www.cnblogs.com/xinaixia/p/4834271.html
總結
以上是生活随笔為你收集整理的C# 之 Math取整的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pigz 压缩
- 下一篇: python-study-17