C# 取整函數


向上取整
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#取整函數有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM