1.使用占位符: 1)float f = 321.12345F;f.ToString("0.00");这样做无论f是不是整数,都将加上2位小数. 2)float f = 321.12345F;f.ToString(".##");这样做最多显示2位小数 占位符的定义如下: (0) Digit placeholder. Display a digit or a zero. If the expression has a digit in the position whe
默认会四舍五入 比如:%0.2f 会四舍五入后,保留小数点后2位 print(string.format("%.1f",0.26)) ---会输出0.3,而不是0.2 Lua保留一位小数 --- nNum 源数字 --- n 小数位数 function Tool. GetPreciseDecimal(nNum, n) if type(nNum) ~= "number" then return nNum; end n = n or 0; n = math.floor(
方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP).doubleValue(); 输出结果f1为 3.15: 源码解读: public BigDecimal setScale(int newScale, int roundingMode) //int newScale 为小数点后保留的位数, int roundingMode 为变量进行取舍的方式: BigDecimal.ROU
toFixed(num)法可把 Number 四舍五入为指定小数位数的数字. num为需要固定的位数 var num=2;console.log(num.toFixed(2));//2.00;var num1=2.011;console.log(num1.toFixed(2));//2.01;var num2=2.019;console.log(num2.toFixed(2));//2.02;注意:num和固定的位数必须是数字,是其他的会报错或是不执行