例:
var num = new Number(13.3714);
document.write(num.toFixed());
document.write(num.toFixed(1));
document.write(num.toFixed(3));
document.write(num.toFixed(10));
結果:
var num = new Number(13.3714);
document.write(num.toFixed());
document.write(num.toFixed(1));
document.write(num.toFixed(3));
document.write(num.toFixed(10));
結果:
13
13.4
13.371
13.3714000000
13.4
13.371
13.3714000000
1.使用Math.floor()無條件捨去
例:
var num = new Number(13.3714);
Math.floor(num*100)/100;
Math.floor(num*10)/10;
結果:
13.37
13.3
2.使用Math.ceil()無條件進位
例:
var num = new Number(13.3714);
Math.ceil(num*100)/100;
Math.ceil(num*10)/10;
結果:
13.38
13.4
Reference:
JavaScript Regular Expressions
回覆刪除