当前位置: 技术问答>java相关
JAVA中有无系统提供的上取整,下取整,四舍五入的函数?
来源: 互联网 发布时间:2015-02-13
本文导语: 多谢指教! | java.lang.math中的几个函数即可: 四舍五入: static long round(double a) Returns the closest long to the argument. static int round(float a) Returns the closest...
多谢指教!
|
java.lang.math中的几个函数即可:
四舍五入:
static long round(double a)
Returns the closest long to the argument.
static int round(float a)
Returns the closest int to the argument.
下取整:
static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
上取整:
static double floor(double a)
Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
例如:double d = math.floor(23.34);
四舍五入:
static long round(double a)
Returns the closest long to the argument.
static int round(float a)
Returns the closest int to the argument.
下取整:
static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
上取整:
static double floor(double a)
Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
例如:double d = math.floor(23.34);
|
import java.lang.*;
public class quzheng{
public static void main(String args[]){
//四舍五入
System.out.println(Math.round(3.40d));
System.out.println(Math.round(3.60d));
System.out.println(Math.round(3.40f));
System.out.println(Math.round(3.60f));
//上取整
System.out.println(Math.ceil(3.4));
System.out.println(Math.ceil(3.6));
//下取整
System.out.println(Math.floor(3.40));
System.out.println(Math.floor(3.60));
}
}
public class quzheng{
public static void main(String args[]){
//四舍五入
System.out.println(Math.round(3.40d));
System.out.println(Math.round(3.60d));
System.out.println(Math.round(3.40f));
System.out.println(Math.round(3.60f));
//上取整
System.out.println(Math.ceil(3.4));
System.out.println(Math.ceil(3.6));
//下取整
System.out.println(Math.floor(3.40));
System.out.println(Math.floor(3.60));
}
}