一、實現
在實現Excel導出時,導出列要求使用數值類型,不能通過ToString將原有的decimal先格式化后再導出,
有兩種實現方法,以保留兩位小數為例
方式一:
decimal temp=232.23234234; int decision=2; int calc = (int)Math.Pow(10, decision); temp = Math.Round(temp * calc) / calc;
方式二:
decimal temp=232.23234234; int decision=2; temp = System.Decimal.Round(temp, decision);
二、 其它:
Java中
Math類提供了3個有關取整的方法:ceil()、floor()、round()。
這些方法與他們的英文名字相對應:
1、ceil,天花板,意思就是向上取整,Math.ceil(11.5)的結果為12,Math.ceil(-11.5)的結果為-11。
2、floor,地板,意思就是向下取整,Math.floor(11.5)的結果為11,Math.floor(-11.5)的結果為-12。
3、round,表示四舍五入,算法為:Math.floor(x+0.5),即將原來的數字加上0.5后在向下取整,Math.round(11.5)的結果為12,Math.round(-11.5)的結果為-11。
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