1)如何計(jì)算乘方
題一:3的4次方(不會(huì)打,請(qǐng)?jiān)?==!!!)
3的4次方=3*3*3*3var a = Math.pow(3,4);console.log(a);
說(shuō)明:Math.pow()
是用來(lái)計(jì)算乘方的語(yǔ)法
注意:Math的M是大寫;
題二:3的4*5次方
var a =Math.pow(3,4*5); console.log(a);
2)如何計(jì)算根號(hào)
題目:根號(hào)81
var a = Math.sqrt(81); console.log(a);
變量格式轉(zhuǎn)換
用戶的輸入
var year = prompt("大哥,你啥時(shí)候出生的呀~");var age = 2016-year;console.log("哇塞,大哥你"+age+"歲了呀!");
說(shuō)明:prompt可以等到用戶輸出的內(nèi)容并加以計(jì)算
注意:prompt的內(nèi)容相加是默認(rèn)為字符串類型的
parseInt的用途
字符串轉(zhuǎn)換為數(shù)字
栗一: var a ="100"; var b =parseInt(a);//把a(bǔ)中的字符串"100"轉(zhuǎn)換成數(shù)字100 console.log(b);//輸出數(shù)字100 console.log(typeof b);// number類型
栗二:var a ="1句話都說(shuō)不好大哥你是不是傻";console.log(parseInt(a));
取整
比如:輸出3.1415926var a =3.1415926;console.log(parseInt(a));
說(shuō)明:parseInt既可以把字符串轉(zhuǎn)換為數(shù)值類型,又可以用來(lái)把數(shù)字取整
注意:取整不會(huì)四舍五入
總結(jié)
以上就是關(guān)于JS中的運(yùn)算符乘方、開方及變量格式轉(zhuǎn)換的全內(nèi)容,希望本文對(duì)大家學(xué)習(xí)Javascript有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注