請問一下,如果我要四捨五路某個float類型的數字到小數點後兩位,然後小數點後的0不秀
例如:
1234.567 --> 1234.57
1234.560 --> 1234.56
1234.507 --> 1234.51
1234.067 --> 1234.07
1234.500 --> 1234.5
1234.007 --> 1234.01
1234.000 --> 1234
在Javascript, Java, Jsp各怎麼做?
謝謝大家~
------解决方案--------------------------------------------------------
java:
double ori = 35454.3636;
double v = Math.round(ori * 100) ;
v = v/100;
System.out.println("v==="+v);
javascript:
var ori = 35454.3636;
var v = Math.round(ori * 100) ;
v = v/100;
alert(v);
------解决方案--------------------------------------------------------
js实现float类型四舍五入取前两位
------解决方案--------------------------------------------------------
toFixed方法
或者
Math.round(a*100)/100.00
------解决方案--------------------------------------------------------
Math.ceil(num * 100)/100
------解决方案--------------------------------------------------------
n = 1.235
var m = parseInt(Math.round(n*100))/100;
------解决方案--------------------------------------------------------
toFixed方法
<script>
var a = 1.436;
alert(a.toFixed(2));
</script>