這篇“js中如何將小數轉為整數”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“js中如何將小數轉為整數”文章吧。
1、使用“parseInt(小數值)”語句。
document.write(parseInt("10") + " "); document.write(parseInt("10.33") + " "); document.write(parseInt("34 45 66") + " "); document.write(parseInt(" 60 ") + " "); document.write(parseInt("40 years") + " "); document.write(parseInt("He was 40") + " "); document.write(" "); document.write(parseInt("10",10)+ " "); document.write(parseInt("010")+ " "); document.write(parseInt("10",8)+ " "); document.write(parseInt("0x10")+ " "); document.write(parseInt("10",16)+ " ");
2、使用“~~小數值”語句。
var decimal=4; var integer = ~~decimal; // 4 = ~~4.123 console.log(integer);
3、使用“Math.floor(小數值)”語句。
console.log(Math.floor(2.5)); //2 console.log(Math.floor(-2.5)); //-3
4、使用“Math.ceil(小數值)”語句。
console.log(Math.ceil(2.5)); //3 console.log(Math.ceil(-2.5)); //-2
5、使用“Math.round(小數值)”語句。
console.log(Math.round(2.5)); //3 console.log(Math.round(-2.5)); //-2 console.log(Math.round(-2.6)); //-3
以上就是關于“js中如何將小數轉為整數”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。