当前位置:编程学习 > JAVA >>

java 中 Math.round() Math.ceil() Math.floor()区别

答案:打一个简单的比如有两个小数12.1和12.7 Math.round()分别是12,和13 -------取最近的值 Math.ceil()分别是12和12 ------取地板 Math.floor()分别是13和13 ------取天花板
其他:ceil()是天花板,即向上取整。floor是地板,向下取整。round是四舍五入。 %:余数
==:地址比较
&&:逻辑与
||:逻辑或
Math.sqrt(x):平方根
Math.pow(x,y):x的y次方
Math.floor(x):舍去小数部分
Math.round(x):四舍五入
 %:余数
==:地址比较
&&:逻辑与
||:逻辑或
Math.sqrt(x):平方根
Math.pow(x,y):x的y次方
Math.floor(x):舍去小数部分
Math.round(x):四舍五入
 %:余数
==:地址比较
&&:逻辑与
||:逻辑或
Math.sqrt(x):平方根
Math.pow(x,y):x的y次方
Math.floor(x):舍去小数部分
Math.round(x):四舍五入
 打一个简单的比如有两个小数12.1和12.7
Math.round()分别是12,和13         -------取最近的值
Math.ceil()分别是12和12              ------取天花板

Math.floor()分别是13和13            ------取地板 错了。 public class asdasd {
	public static void main(String[] args) {
System.out.println(Math.round(12.6)) ;   //四舍五入
System.out.println(Math.ceil(12.6));      //向上取整(天花板)   
System.out.println(Math.floor(12.6));   //向下取整(地板)
}
}
打印结果:13       13      12 Math.floor和Math.ceil 截断数字的两个函数 
/** *这两个函数的主要任务是截掉小数以后的位数. 
*区别是: floor()总是把数字变得越来越小,而ceil()总是把数字变大。 
*其实名字可以理解floor()是地板,ceil()是天花板。 
Math.round()将小数值舍入到最接近的整数。 

上一个:java编译还是解释
下一个:javaWeb工程提示:org.apache.jasper.JasperException: Unable to compile class for JSP:

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,