JS的Math():四舍五入、向下取整、向上取整、隨機數、絕對值、最小值、最大值
阿新 • • 發佈:2019-04-03
ceil abs 返回 整數 floor col color math 小數
1 // 1.四舍五入 2 Math.round(1.4); // 1 3 Math.round(1.5); // 2 4 5 // 2.向下取整(返回比參數小且最相鄰的整數,註意:負小數易出錯) 6 Math.floor(1.234); //1 7 Math.floor(-1.234); //-2 8 9 // 3.向上取整(返回比參數大且最相鄰的整數,註意:負小數易出錯) 10 Math.ceil(1.234);//2 11 Math.ceil(-1.234);//-1 12 13 // 4.返回0-1之間的隨機數 14 Math.random(); 15 16 // 5.絕對值17 Math.abs(1); // 1 18 Math.abs(-1); // 1 19 20 // 6.返回兩者中的較小值 21 Math.min(1, 2); // 1 22 23 // 7.返回兩者中的較大值 24 Math.max(1, 2); // 2
JS的Math():四舍五入、向下取整、向上取整、隨機數、絕對值、最小值、最大值