今天上班臨時被同事問題這個Item要怎樣寫,一時還真的忘記要如何寫,腦中只要很笨的substring方式。
後來,頭腦清醒了一下,大概把寫法記錄下來,以免之後又忘記了
1.無條件進位
1 |
double s = 100; |
2 |
int result = 0; |
3 |
result = Convert.ToInt16(Math.Ceiling(s / 3)); |
2.無條件捨去
1 |
double s = 100; |
2 |
int result = 0; |
3 |
result =Convert.ToInt16( Math.Floor(s / 3)); |
3.四捨五入
用法:Math.Round(計算值,小數點第幾位)
1 |
double s = 110; |
2 |
double result = 0; |
3 |
result = Math.Round((s / 3), 2); |
4 |
5 |
亂馬客之提醒:若是要呈現一般認知的四捨五入需加入第三個參數-MidpointRounding.AwayFromZer |
6 |
Example:System.Math.Round(1.235 , 2, MidpointRounding.AwayFromZero) |
















