自己寫了個 四舍五入的函數.
//取小數後第一位,四舍五入
function Get_decimal(decimalvalue){
var bbb=decimalvalue+"";
var shu=bbb.split(".");
if(shu.length==1){
return decimalvalue;
}
if(shu.length==2){
var first=shu[0];
var second=shu[1];
//小樹後第一位
var secondone=second.substring(0,1);
var secondtwo=second.substring(1,2);
//如果小數第一位小於9
if(secondone<9){
if(secondtwo>=5){
secondone=parseInt(secondone,10)+1;
}
}else{
if(secondtwo>=5){
first=parseInt(first,10)+1;
secondone=0;
}
}
return first+"."+secondone;
}
}
昨天看書,js竟然有四舍五入的函數,無語了,哈哈誰怪自己見識短呢......
<html>
<head><title>ssss</title></head>
<script>
function toFix(idvalue){
alert(parseFloat(idvalue).toFixed(2));//2表示保留2位小數
}
</script>
<body>
<table><tr><td>
<input type="text" name="aaa" οnchange="toFix(this.value)">
</td></tr></table>
</body>
</html>