久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      javascript怎么將小數(shù)轉(zhuǎn)換為整數(shù)

      JS將小數(shù)轉(zhuǎn)為整數(shù)的方法:1、使用“parseInt(小數(shù)值)”語(yǔ)句;2、使用“~~小數(shù)值”語(yǔ)句;3、使用“Math.floor(小數(shù)值)”語(yǔ)句;4、使用“Math.ceil(小數(shù)值)”語(yǔ)句;5、使用“Math.round(小數(shù)值)”語(yǔ)句。

      javascript怎么將小數(shù)轉(zhuǎn)換為整數(shù)

      本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

      方法1:使用 parseInt()

      parseInt() 函數(shù)可解析一個(gè)字符串,并返回一個(gè)整數(shù)。

      當(dāng)參數(shù) radix 的值為 0,或沒(méi)有設(shè)置該參數(shù)時(shí),parseInt() 會(huì)根據(jù) string 來(lái)判斷數(shù)字的基數(shù)。

      當(dāng)忽略參數(shù) radix , JavaScript 默認(rèn)數(shù)字的基數(shù)如下:

      • 如果 string 以 "0x" 開(kāi)頭,parseInt() 會(huì)把 string 的其余部分解析為十六進(jìn)制的整數(shù)。

      • 如果 string 以 0 開(kāi)頭,那么 ECMAScript v3 允許 parseInt() 的一個(gè)實(shí)現(xiàn)把其后的字符解析為八進(jìn)制或十六進(jìn)制的數(shù)字。

      • 如果 string 以 1 ~ 9 的數(shù)字開(kāi)頭,parseInt() 將把它解析為十進(jìn)制的整數(shù)。

      示例:使用 parseInt() 來(lái)解析不同的字符串

      document.write(parseInt("10") + "<br>"); document.write(parseInt("10.33") + "<br>"); document.write(parseInt("34 45 66") + "<br>"); document.write(parseInt(" 60 ") + "<br>"); document.write(parseInt("40 years") + "<br>"); document.write(parseInt("He was 40") + "<br>");   document.write("<br>"); document.write(parseInt("10",10)+ "<br>"); document.write(parseInt("010")+ "<br>"); document.write(parseInt("10",8)+ "<br>"); document.write(parseInt("0x10")+ "<br>"); document.write(parseInt("10",16)+ "<br>");

      輸出結(jié)果:

      10 10 34 60 40 NaN  10 10 8 16 16

      方法2:兩次取反

      var decimal=4; var integer = ~~decimal; // 4 = ~~4.123 console.log(integer);

      輸出結(jié)果:

      4

      方法3:Math.floor()向下取整

      Math.floor():返回小于參數(shù)值的最大整數(shù)。

      console.log(Math.floor(2.5));  //2 console.log(Math.floor(-2.5));  //-3

      方法4:Math.ceil()向上取整

      Math.ceil():返回大于參數(shù)值的最小整數(shù)。

      console.log(Math.ceil(2.5));  //3 console.log(Math.ceil(-2.5));  //-2

      方法5:Math.round()四舍五入

      Math.round():四舍五入。

      console.log(Math.round(2.5));  //3 console.log(Math.round(-2.5));  //-2 console.log(Math.round(-2.6));  //-3

      【推薦學(xué)習(xí):javascript高級(jí)教程】

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)