1.取余數 %
var a=10%3; //a=1
2.取絕對值 Math.abs()
var a=Math.abs(-102.1); var b=Math.abs(102.1); //a=102.1;b=102.1
3.截取小數點后長度並進行四舍五入 toFixed()
var num_1 = new Number(13.53); alert(num_1.toFixed(1)); alert(num_1.toFixed(0)); //13.5 //14
var num_2 = new Number(13.56); alert(num_2.toFixed(1)); alert(num_2.toFixed(0)); //13.6 //14
4.取最小值 Math.min()
Math.min(5,7) Math.min(-3,5) Math.min(-3,-5) (Math.min(7.25,7.30) //結果 //5 //-3 //-5 //7.25
5.取最大值 Math.max()
Math.max(5,7,9) Math.max(-3,5) Math.max(-3,-5) Math.max(7.25,7.30) //結果 //9 //5 //-3 //7.30
6.指數計數法 和 截取數字固定長度 toPrecision()
var a=1000.25; alert(a.toPrecision(1)) alert(a.toPrecision(2)) alert(a.toPrecision(4)) alert(a.toPrecision(6)) alert(a.toPrecision(8)) //結果 //1e+3 //1.0e+3 //1000 //1000.25 //1000.2500
7.把一個數字舍入為最接近的整數 round()
Math.round(0.60) Math.round(0.50) Math.round(0.49) Math.round(-4.40) Math.round(-4.60) //結果 //1 //1 //0 //-4 //-5