Math 對(duì)象

Math 對(duì)象

Math 對(duì)象用于執(zhí)行數(shù)學(xué)任務(wù)

Math 對(duì)象并不像 Date 和 String 那樣是對(duì)象的類,因此沒(méi)有構(gòu)造函數(shù) Math()

常用的 Math () 方法

常用的取整方法

ceil()

Math.ceil() 方法可對(duì)一個(gè)數(shù)進(jìn)行上舍入

var num = 0.4
Math.ceil(num)

console.log(num,Math.ceil(0.4))  //1

floor()

Math.floor() 方法可以向下取整

console.log(Math.floor(0.4))  //0

round()

Math.round() 方法可以四舍五入取整

console.log(Math.round(0.4))  //0

常用方法

abs()

Math.abs() 方法可以返回絕對(duì)值

console.log(Math.abs(-0.4))  //0.4

random()

Math.random() 方法可以返回一個(gè) 0 - 1 的隨機(jī)數(shù)

console.log(Math.round(Math.random()*10))
//0-10的隨機(jī)數(shù)

PI

Math.PI 方法可以返回絕對(duì)值

console.log(Math.PI))  //3.141592653589793

max()

Math.max() 函數(shù)返回一組數(shù)中的最大值。

console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.max(-1, -3, -2));
// expected output: -1

const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// expected output: 3

min()

Math.min() 返回零個(gè)或更多個(gè)數(shù)值的最小值。

console.log(Math.max(1, 3, 2));
// expected output: 1

console.log(Math.max(-1, -3, -2));
// expected output: -3

const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// expected output: 1

使用 Math.min() 裁剪值

var x = Math.min(f(foo), boundary);
// 等同于
// var x = f(foo);
// if (x > boundary) {
//     x = boundary;
// }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容