java Math.floor,Math.ceil,Math.rint,Math.round用法详解
来源:互联网 发布:美国网络存储公司 编辑:程序博客网 时间:2024/06/04 17:43
Math.floor函数讲解
floor原意:地板。Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数。看下面的例子:
public class Test{ public static void main(String[] args) { System.out.println("Math.floor(-1.1): " + Math.floor(-1.1)); System.out.println("Math.floor(-1.5): " + Math.floor(-1.5)); System.out.println("Math.floor(-1.6): " + Math.floor(-1.6)); System.out.println("Math.floor(0.1): " + Math.floor(0.1)); System.out.println("Math.floor(0.5): " + Math.floor(0.5)); System.out.println("Math.floor(0.6): " + Math.floor(0.6)); System.out.println("Math.floor(1.1): " + Math.floor(1.1)); System.out.println("Math.floor(1.5): " + Math.floor(1.5)); System.out.println("Math.floor(1.6): " + Math.floor(1.6)); }}
结果为:
Math.floor(-1.1): -2.0
Math.floor(-1.5): -2.0
Math.floor(-1.6): -2.0
Math.floor(0.1): 0.0
Math.floor(0.5): 0.0
Math.floor(0.6): 0.0
Math.floor(1.1): 1.0
Math.floor(1.5): 1.0
Math.floor(1.6): 1.0
Math.ceil函数讲解
ceil愿意:天花板。Math.ceil函数执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。看下面的例子:
public class Test{ public static void main(String[] args) { System.out.println("Math.ceil(-1.1): " + Math.ceil(-1.1)); System.out.println("Math.ceil(-1.5): " + Math.ceil(-1.5)); System.out.println("Math.ceil(-1.6): " + Math.ceil(-1.6)); System.out.println("Math.ceil(0.1): " + Math.ceil(0.1)); System.out.println("Math.ceil(0.5): " + Math.ceil(0.5)); System.out.println("Math.ceil(0.6): " + Math.ceil(0.6)); System.out.println("Math.ceil(1.1): " + Math.ceil(1.1)); System.out.println("Math.ceil(1.5): " + Math.ceil(1.5)); System.out.println("Math.ceil(1.6): " + Math.ceil(1.6)); }}
结果为:
Math.ceil(-1.1): -1.0
Math.ceil(-1.5): -1.0
Math.ceil(-1.6): -1.0
Math.ceil(0.1): 1.0
Math.ceil(0.5): 1.0
Math.ceil(0.6): 1.0
Math.ceil(1.1): 2.0
Math.ceil(1.5): 2.0
Math.ceil(1.6): 2.0
Math.rint函数讲解
Math.rint函数返回最接近参数的整数,如果有2个数同样接近,则返回偶数的那个。
public class Test{ public static void main(String[] args) { System.out.println("Math.rint(-1.1): " + Math.rint(-1.1)); System.out.println("Math.rint(-1.5): " + Math.rint(-1.5)); System.out.println("Math.rint(-1.6): " + Math.rint(-1.6)); System.out.println("Math.rint(0.1): " + Math.rint(0.1)); System.out.println("Math.rint(0.5): " + Math.rint(0.5)); System.out.println("Math.rint(0.6): " + Math.rint(0.6)); System.out.println("Math.rint(1.1): " + Math.rint(1.1)); System.out.println("Math.rint(1.5): " + Math.rint(1.5)); System.out.println("Math.rint(1.6): " + Math.rint(1.6)); }}
结果为:
Math.rint(-1.1): -1.0
Math.rint(-1.5): -2.0
Math.rint(-1.6): -2.0
Math.rint(0.1): 0.0
Math.rint(0.5): 0.0
Math.rint(0.6): 1.0
Math.rint(1.1): 1.0
Math.rint(1.5): 2.0
Math.rint(1.6): 2.0
Math.round函数讲解
round方法,它表示”四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整
public class Test{ public static void main(String[] args) { System.out.println("Math.round(-1.1): " + Math.round(-1.1)); System.out.println("Math.round(-1.5): " + Math.round(-1.5)); System.out.println("Math.round(-1.6): " + Math.round(-1.6)); System.out.println("Math.round(0.1): " + Math.round(0.1)); System.out.println("Math.round(0.5): " + Math.round(0.5)); System.out.println("Math.round(0.6): " + Math.round(0.6)); System.out.println("Math.round(1.1): " + Math.round(1.1)); System.out.println("Math.round(1.5): " + Math.round(1.5)); System.out.println("Math.round(1.6): " + Math.round(1.6)); }}
结果为:
Math.round(-1.1): -1
Math.round(-1.5): -1
Math.round(-1.6): -2
Math.round(0.1): 0
Math.round(0.5): 1
Math.round(0.6): 1
Math.round(1.1): 1
Math.round(1.5): 2
Math.round(1.6): 2
转载自:Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- java Math.floor,Math.ceil,Math.rint,Math.round用法详解
- java Math floor ceil rint round
- Java Math的floor、ceil、rint及round用法
- Math.ceil、Math.floor、Math.round用法
- Java Math的floor、ceil、rint及round方法
- Math.ceil()和Math.floor()、Math.round()
- Math.round|Math.floor|Math.ceil区别
- Math.floor,Math.round,Math.ceil
- Math.round, Math.ceil 和 Math.floor
- Math.floor()/Math.ceil()/Math.round()
- JavaScript中的Math.ceil()、Math.floor()、Math.round()详解
- java 中 Math.round() Math.ceil() Math.floor()区别
- java中Math.round(),Math.ceil(),Math.floor()运算
- Linux 下面安装redis
- Seek the Name, Seek the Fame
- UVA-11000 Bee-蜜蜂繁殖
- 【XML】视频总结
- uvalive 3616
- java Math.floor,Math.ceil,Math.rint,Math.round用法详解
- POJ-2013 Symmetric Order-对称排列人名
- 验证邮箱是否存在原理
- POJ - 2155 Matrix (二维树状数组 + 区间修改 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)
- C#高级编程----反射的小结
- 线程的三种创建方法
- 基本命令(按学习进度随机添加)
- ORM进阶之 ORM简介
- poj 2752 Seek the Name, Seek the Fame 【在原串中 找所有既是原串前缀又是原串后缀的子串长度】