[C#] 數字的無條件進位/無條件捨去/四捨五入
無條件進位 (Math.Ceiling)double src = 100;int res = 0;
res = Convert.ToInt16(Math.Ceiling(src/3));輸出 34
無條件捨去 (Math.Floor)double src = 100;
int res = 0;
res = Convert.ToInt16(Math.Floor(src/3));輸出 33
四捨五入double src = 100;
int res = 0;
res = Convert.ToInt16(Math.Round(src/3));輸出 33double src = 100;
double res = 0;
res = Math.Round(src/3, 2); // 取到小數下第二位輸出 33.33
另加收錄,將數字前幾位補0變字串int src = 66;
string s = src.ToString().PadLeft(4, '0');輸出字串 0066
頁:
[1]