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);
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));
}
}