美文网首页
2018-04-24

2018-04-24

作者: nikown | 来源:发表于2018-04-24 21:18 被阅读0次

Java学习随笔7

Math.round以及Math.floor

这俩函数都可以消除double的小数部分,但是各自产生的结果不同。

Math.round: 

对于一个double,如果double>0,那么采用的是四舍五入。

double temp1=1.5;

double temp2=1.4;

那么Math.round(temp1)=2       Math.round(temp2)=1

对于一个double<0,那么采用的是五舍六入。

double temp3=-1.5;

double temp4=-1.6;

那么Math.round(temp3)=-1       Math.round(temp4)=-2

Math.floor:

这个函数比较简单一点,直接取比double小,且离double最近的数。

double temp5=1.1;

double temp6=-1.1;

那么Math.round(temp5)=1       Math.round(temp6)=-2

相关文章

网友评论

      本文标题:2018-04-24

      本文链接:https://www.haomeiwen.com/subject/fdrslftx.html