溫馨提示×

溫馨提示×

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

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

?JavaScript進行數(shù)值取整的示例分析

發(fā)布時間:2021-03-05 11:26:28 來源:億速云 閱讀:103 作者:小新 欄目:web開發(fā)

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

js取整數(shù)的方法:1、通過“Math.trunc()”方法去除數(shù)字的小數(shù)部分,保留整數(shù)部分;2、通過“Math.round()”方法返回一個數(shù)字四舍五入后的整數(shù)部分;3、通過“Math.ceil()”方法實現(xiàn)向上取整等等。

JavaScript進行數(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()方法返回一個數(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()方法返回一個大于或等于數(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()方法返回一個小于或等于數(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進行數(shù)值取整的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

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

AI