博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java floor round ceil 使用
阅读量:6953 次
发布时间:2019-06-27

本文共 1335 字,大约阅读时间需要 4 分钟。

1、round使用System.out.println("Math.round=="+Math.round(11.46)+"=="+Math.round(11.50)+"=="+Math.round(11.76)+"=="+Math.round(-11.46)+"=="+Math.round(-11.76)+"=="+Math.round(-11.50));

----Math.round==11==12==12==-11==-12==-11

总结:Math.round()       
1)、  四舍五入的取整(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。

2)、Math.round(Math.random()*(y-x))+x;   返回x~y的随机数,包含负数。

2、floor使用System.out.println("Math.floor=="+Math.floor(11.46)+"=="+Math.floor(11.50)+"=="+Math.floor(11.76)+"=="+Math.floor(-11.46)+"=="+Math.floor(-11.76)+"=="+Math.floor(-11.50));

-----Math.floor==11.0==11.0==11.0==-12.0==-12.0==-12.0

总结:Math.floor()(小于等于 x,且与 x 最接近的整数。)

其实返回值就是该数的整数位:
Math.floor(0.666)   -->  0
Math.floor(39.2783)   -->  39

所以我们可以使用Math.floor(Math.random())去获取你想要的一个范围内的整数。

如:现在要从1~52内取一个随机数:
首先Math.random()*52  //这样我们就能得到一个 >=0 且 <52的数
然后加1:Math.random()*52 + 1    //现在这个数就 >=1 且 <53
再使用Math.floor取整
最终: Math.floor(Math.random()*52 + 1)

这就能得到一个取值范围为1~52的随机整数了.

3、ceil使用System.out.println("Math.ceil=="+Math.ceil(11.46)+"=="+Math.ceil(11.50)+"=="+Math.ceil(11.76)+"=="+Math.ceil(-11.46)+"=="+Math.ceil(-11.76)+"=="+Math.ceil(-11.50));

-----Math.ceil==12.0==12.0==12.0==-11.0==-11.0==-11.0

总结:向上取整,如Math.cell(0.3)=1 、又如Math.ceil(Math.random()*10) 返回1~10

4、random使用

Math.random()       返回值是一个大于等于0,且小于1的随机数

Math.random()*N    返回值是一个大于等于0,且小于N的随机数

 

转载于:https://www.cnblogs.com/523823-wu/p/9944177.html

你可能感兴趣的文章
需求改进与系统设计
查看>>
jquery特效 商品SKU属性规格选择实时联动
查看>>
HashMap[转]
查看>>
面向对象程序设计——总结作业
查看>>
linux之 sed命令
查看>>
oracle rac的特征
查看>>
Linux之 find之 ctime,atime,mtime
查看>>
mysql查询
查看>>
Mongodb JAVA API
查看>>
MacFree ePlicy Orchestrator
查看>>
python基础之列表生成式和生成器
查看>>
关于java.lang.ClassNotFoundException
查看>>
转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
查看>>
一条SQL语句查询出成绩名次 排名 (转)
查看>>
什么是.NET应用程序域
查看>>
ZooKeeper 安装与部署
查看>>
Picnic Planning
查看>>
文章标题
查看>>
约瑟夫问题
查看>>
NinePatchChunk.java分析
查看>>