JavaScript四種數值取整方法

來源:互聯網
上載者:User

標籤:round   ceil   script   四捨五入   大於   java   markdown   最小   log   

一、Math.trunc()1.定義

Math.trunc()方法去除數位小數部分,保留整數部分。

2.文法
Math.trunc(value)
3.樣本
console.log(Math.trunc(2.01)); // 2console.log(Math.trunc(2.9)); // 2console.log(Math.trunc('0.22')); // 0console.log(Math.trunc(-1.22)); // -1console.log(Math.trunc(-1.56)); // -1console.log(Math.trunc(true)); // 1console.log(Math.trunc(undefined)); // NaN
二、Math.round()1.定義

Math.round()方法返回一個數字四捨五入後的整數部分。

2.文法
Math.round(value)
3.樣本
console.log(Math.round(2.01)); // 2console.log(Math.round(2.9)); // 3console.log(Math.round('0.22')); // 0console.log(Math.round(-1.22)); // -1console.log(Math.round(-1.56)); // -2console.log(Math.round(true)); // 1console.log(Math.round(undefined)); // NaN
三、Math.ceil()1.定義

Math.ceil()方法返回一個大於或等於數位最小整數,即向上取整。

2.文法
Math.ceil(value)
3.樣本
console.log(Math.ceil(2.01)); // 3console.log(Math.ceil(2.9)); // 3console.log(Math.ceil('0.22')); // 1console.log(Math.ceil(-1.22)); // -1console.log(Math.ceil(-1.56)); // -1console.log(Math.ceil(true)); // 1console.log(Math.ceil(undefined)); // NaN
四、Math.floor()1.定義

Math.floor()方法返回一個小於或等於數位最小整數,即向下取整。

2.文法
Math.floor(value)
3.樣本
console.log(Math.floor(2.01)); // 2console.log(Math.floor(2.9)); // 2console.log(Math.floor('0.22')); // 0console.log(Math.floor(-1.22)); // -2console.log(Math.floor(-1.56)); // -2console.log(Math.floor(true)); // 1console.log(Math.floor(undefined)); // NaN

JavaScript四種數值取整方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.