• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

JS 小数点 取整

Data: 2016-03-27 11:59:04Form: JournalClick: 9

JS 小数点 取整
JS 小数点 取整_百度知道

function abc(n){

return Math.ceil(n);

}

//Math.ceil() 向上取整

//Math.floor() 向下取整

//Math.round() 四舍五入

 

 

这个不用写函数,js里有专门的函数,Math.ceil(num),功能是对于小数上取整 ,比如Math.ceil(3.11),返回4

 

1.

丢弃

小数部

,保留整

2113

数部分

eg:parseInt(5/2)

2.向上取整

5261

,有小数就整数部

4102

分加1

eg:Math.ceil(5/2)

3.四舍

五入

.

eg:Math.round(5/2)

4.向下取整

eg:Math.floor(5/2)

举例:

aa=parseInt(5/2)

alert("取整"+aa);

//2(

丢掉1653

小数部分)

bb=Math.ceil(5/2)

alert("ceil"+bb);

//3(向上取整)

cc=Math.round(5/2);

alert("round"+cc);

//3(四舍五入)

dd=Math.floor(5/2);

alert("floor"+dd);

//2(向下取整)

 

 

 

Name:
<提交>