這篇文章主要介紹JavaScript進(jìn)行數(shù)值取整的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)永仁免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
js取整數(shù)的方法:1、通過“Math.trunc()”方法去除數(shù)字的小數(shù)部分,保留整數(shù)部分;2、通過“Math.round()”方法返回一個(gè)數(shù)字四舍五入后的整數(shù)部分;3、通過“Math.ceil()”方法實(shí)現(xiàn)向上取整等等。
JavaScript進(jìn)行數(shù)值取整:
一、Math.trunc()
1、定義
Math.trunc()方法去除數(shù)字的小數(shù)部分,保留整數(shù)部分。
2、語法
Math.trunc(value)
3、示例
console.log(Math.trunc(2.01)); // 2
console.log(Math.trunc(2.9)); // 2
console.log(Math.trunc('0.22')); // 0
console.log(Math.trunc(-1.22)); // -1
console.log(Math.trunc(-1.56)); // -1
console.log(Math.trunc(true)); // 1
console.log(Math.trunc(undefined)); // NaN二、Math.round()
1.定義
Math.round()方法返回一個(gè)數(shù)字四舍五入后的整數(shù)部分。
2、語法
Math.round(value)
3、示例
console.log(Math.round(2.01)); // 2
console.log(Math.round(2.9)); // 3
console.log(Math.round('0.22')); // 0
console.log(Math.round(-1.22)); // -1
console.log(Math.round(-1.56)); // -2
console.log(Math.round(true)); // 1
console.log(Math.round(undefined)); // NaN三、Math.ceil()
1、定義
Math.ceil()方法返回一個(gè)大于或等于數(shù)字的最小整數(shù),即向上取整。
2、語法
Math.ceil(value)
3、示例
console.log(Math.ceil(2.01)); // 3
console.log(Math.ceil(2.9)); // 3
console.log(Math.ceil('0.22')); // 1
console.log(Math.ceil(-1.22)); // -1
console.log(Math.ceil(-1.56)); // -1
console.log(Math.ceil(true)); // 1
console.log(Math.ceil(undefined)); // NaN四、Math.floor()
1、定義
Math.floor()方法返回一個(gè)小于或等于數(shù)字的最小整數(shù),即向下取整。
2、語法
Math.floor(value)
3、示例
console.log(Math.floor(2.01)); // 2
console.log(Math.floor(2.9)); // 2
console.log(Math.floor('0.22')); // 0
console.log(Math.floor(-1.22)); // -2
console.log(Math.floor(-1.56)); // -2
console.log(Math.floor(true)); // 1
console.log(Math.floor(undefined)); // NaN以上是“JavaScript進(jìn)行數(shù)值取整的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:?JavaScript進(jìn)行數(shù)值取整的示例分析
網(wǎng)站路徑:http://www.chinadenli.net/article38/iiissp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、自適應(yīng)網(wǎng)站、App開發(fā)、網(wǎng)站營銷、企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)