溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

JavaScript中Math對(duì)象怎么用

發(fā)布時(shí)間:2022-03-25 10:05:51 來(lái)源:億速云 閱讀:107 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹了JavaScript中Math對(duì)象怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Math對(duì)象

屬性:
JavaScript中Math對(duì)象怎么用
Math對(duì)象方法:

三角函數(shù)(sin(), cos(), tan(),asin(), acos(), atan(), atan2())是以弧度返回值的??梢酝ㄟ^(guò)除法Math.PI / 180把弧度轉(zhuǎn)換為角度,也可以通過(guò)其他方法來(lái)轉(zhuǎn)換。

方法說(shuō)明
Math.abs(x)返回x的絕對(duì)值.
Math.acos(x)返回x的反余弦值.
Math.acosh(x)返回x的反雙曲余弦值.
Math.asin(x)返回x的反正弦值.
Math.asinh(x)返回x的反雙曲正弦值.
Math.atan(x)以介于 -PI/2 與 PI/2 弧度之間的數(shù)值來(lái)返回 x 的反正切值.
Math.atanh(x)返回 x 的反雙曲正切值.
Math.atan2(x, y)返回 y/x 的反正切值.
Math.cbrt(x)返回x的立方根.
Math.ceil(x)返回x向上取整后的值.
Math.clz32(x)Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x)返回x的余弦值.
Math.cosh(x)返回x的雙曲余弦值.
Math.exp(x)返回 Ex, 當(dāng)x為參數(shù), E 是歐拉常數(shù) (2.718…), 自然對(duì)數(shù)的底.
Math.expm1(x)返回 exp(x)-1 的值.
Math.floor(x)返回小于x的最大整數(shù)。
Math.fround(x)Returns the nearest single precision float representation of a number.
Math.hypot([x[,y[,…]]])Returns the square root of the sum of squares of its arguments.
Math.imul(x)Returns the result of a 32-bit integer multiplication.
Math.log(x)Returns the natural logarithm (loge, also ln) of a number.
Math.log1p(x)Returns the natural logarithm of 1 + x (loge, also ln) of a number.
Math.log10(x)Returns the base 10 logarithm of x.
Math.log2(x)Returns the base 2 logarithm of x.
Math.max([x[,y[,…]]])返回0個(gè)到多個(gè)數(shù)值中最大值.
Math.min([x[,y[,…]]])返回0個(gè)到多個(gè)數(shù)值中最小值.
Math.pow(x,y)返回x的y次冪.
Math.random()返回0到1之間的偽隨機(jī)數(shù). 可能等于0,但是一定小于1
Math.round(x)返回四舍五入后的整數(shù).但是Math.round(-4.40)值為-4
Math.sign(x)返回x的符號(hào)函數(shù), 判定x是正數(shù),負(fù)數(shù)還是0.
Math.sin(x)返回正弦值.
Math.sinh(x)返回x的雙曲正弦值.
Math.sqrt(x)返回x的平方根.
Math.tan(x)返回x的正切值.
Math.tanh(x)返回x的雙曲正切值.
Math.toSource()返回字符串 “Math”.
Math.trunc(x)返回x的整數(shù)部分,去除小數(shù).

例子1:寫(xiě)一個(gè)函數(shù),返回從min到max之間的隨機(jī)整數(shù),包括min不包括max

function getRandomArbitrary(min, max) {
  return min + Math.random() * (max - min);}

例子2:寫(xiě)一個(gè)函數(shù),生成一個(gè)長(zhǎng)度為 n 的隨機(jī)字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z

function getRandomInt(min, max) {
  return min + Math.floor(Math.random() * (max - min + 1));}function randomStr(n){
  var dict = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var str = '';
  for(i = 0; i < n;i++){
    str += dict[getRandomInt(0,62)];
  }
  return str;}var str = getRandStr(10);console.log(str);

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“JavaScript中Math對(duì)象怎么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI