当前位置:首页>开发>正文

java几个小数取整例子 JS小数点怎么取整

2023-06-21 10:51:23 互联网 未知 开发

 java几个小数取整例子 JS小数点怎么取整

java几个小数取整例子

(要学会百度啊)
Math类中提供的三个与取整有关的方法:
第一个: ceil
ceil的意思就是: 天花板的意思该方法表示的是向上取整Math.ceil(11.3)的值是12 Math.ceil.(-11.6)的结果是-11
第二个是: floor
首先他的英文含义就是地板的含义,该方法就表示的是向下取整, 
Math.floor(11.6)的结果就是11  
Math.floor(-11.4)的结果就是-1第三个是: round
他表示的是四舍五入,算法为 Math.floor(x 0.5)也就是在原来的数字上加上0.5之后再进行向下取整 
Math.round(11.5)也就是 Math.floor(11.5 0.5)= Math.floor(12)=12 
同理: Math.round(-11.5)= Math.floor(-11.5 0.5)=Math.floor(-11.0)= -11
原文地址:网页链接

JS小数点怎么取整?

1.丢弃小数部分,保留整数部分   eg:parseInt(5/2)
2.向上取整,有小数就整数部分加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(丢掉小数部分)
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(向下取整)

最新文章