java中round()函数,floor()函数,ceil()函数的返回值
2017-08-13 17:16
399 查看
不太熟悉的是round()函数的一些边缘值,比如Math.round(11.5)是多少,所以测试了一下。当前,之前对于向上取整和向下取整也有误解地方,一直以为返回数字应该为int类型,但是看了源码才知道返回值是double类型。
测试代码:
/**
* created by cxh on 17/7/27
*/
public class TempTest {
public static void main(String[] args) {
/**
* 测试Math.round(x)输出数字;他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整
* 如果x距离相邻两侧的整数距离不一样,则取距离近的那个数字;
* 如果x距离相邻两侧的整数距离一样,则取真值大的那个数字(即为大于x的那个数字)
*/
System.out.println(Math.round(-11.3));// -11
System.out.println(Math.round(-11.5));//-11
System.out.println(Math.round(-11.6));//-12
System.out.println(Math.round(11.3));//11
System.out.println(Math.round(11.5));//12
System.out.println(Math.round(11.6));//12
/**
* 向下取整,返回的是一个double值
*/
System.out.println(Math.floor(11.8));//11.0
System.out.println(Math.floor(11.5));//11.0
System.out.println(Math.floor(11.1));//11.0
System.out.println(Math.floor(-11.8));//-12.0
System.out.println(Math.floor(-11.5));//-12.0
System.out.println(Math.floor(-11.1));//-12.0
/**
* 向上取整,返回的是一个double值
*/
System.out.println(Math.ceil(11.8));//12.0
System.out.println(Math.ceil(11.5));//12.0
System.out.println(Math.ceil(11.1));//12.0
System.out.println(Math.ceil(-11.8));//-11.0
System.out.println(Math.ceil(-11.5));//-11.0
System.out.println(Math.ceil(-11.1));//-11.0
}
}
测试代码:
/**
* created by cxh on 17/7/27
*/
public class TempTest {
public static void main(String[] args) {
/**
* 测试Math.round(x)输出数字;他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整
* 如果x距离相邻两侧的整数距离不一样,则取距离近的那个数字;
* 如果x距离相邻两侧的整数距离一样,则取真值大的那个数字(即为大于x的那个数字)
*/
System.out.println(Math.round(-11.3));// -11
System.out.println(Math.round(-11.5));//-11
System.out.println(Math.round(-11.6));//-12
System.out.println(Math.round(11.3));//11
System.out.println(Math.round(11.5));//12
System.out.println(Math.round(11.6));//12
/**
* 向下取整,返回的是一个double值
*/
System.out.println(Math.floor(11.8));//11.0
System.out.println(Math.floor(11.5));//11.0
System.out.println(Math.floor(11.1));//11.0
System.out.println(Math.floor(-11.8));//-12.0
System.out.println(Math.floor(-11.5));//-12.0
System.out.println(Math.floor(-11.1));//-12.0
/**
* 向上取整,返回的是一个double值
*/
System.out.println(Math.ceil(11.8));//12.0
System.out.println(Math.ceil(11.5));//12.0
System.out.println(Math.ceil(11.1));//12.0
System.out.println(Math.ceil(-11.8));//-11.0
System.out.println(Math.ceil(-11.5));//-11.0
System.out.println(Math.ceil(-11.1));//-11.0
}
}
相关文章推荐
- Java Math floor round ceil 函数
- Java Math floor round ceil 函数
- Java Math floor round ceil 函数
- [前端技术]如何加深对JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
- Javascript Math ceil()、floor()、round()三个函数的区别
- javascript中的Math.round()和Math.floor()函数Math.ceil()的区别
- matlab中的rem()mod() floor() round() ceil()函数
- matlab中fix, floor, ceil, round 函数的使用方法
- Java Math的 floor,round和ceil的总结
- php取整函数ceil,floor,round,intval函数的区别
- Javascript Math 中常用函数<一>--ceil(),floor(),round()
- Java Math的 floor,round和ceil的总结
- JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
- Java Math的 floor,round和ceil的总结
- java Math.floor,Math.ceil,Math.rint,Math.round用法详解
- Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
- php取整函数ceil,floor,round,intval函数的区别
- JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解
- delphi的函数round、trunc、ceil、floor 和RoundTo
- php取整函数ceil,floor,round,intval函数的区别