java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。

基本运算的方法

public static double abs(double a) :返回 double 值的绝对值。

  1. public class Test {
  2. public static void main(String[] args) {
  3. double d1 = Math.abs(-5.6);
  4. double d2 = Math.abs(5.6);
  5. System.out.println(d1);
  6. System.out.println(d2);
  7. }
  8. }

public static double ceil(double a) :返回大于等于参数的小的整数。

  1. public class Test {
  2. public static void main(String[] args) {
  3. double d1 = Math.ceil(-5.6);
  4. double d2 = Math.ceil(5.6);
  5. System.out.println(d1);
  6. System.out.println(d2);
  7. }
  8. }

public static double floor(double a) :返回小于等于参数大的整数。

  1. public class Test {
  2. public static void main(String[] args) {
  3. double d1 = Math.floor(-5.6);
  4. double d2 = Math.floor(5.6);
  5. System.out.println(d1);
  6. System.out.println(d2);
  7. }
  8. }

public static long round(double a) :返回接近参数的 long。(相当于四舍五入方法)

  1. public class Test {
  2. public static void main(String[] args) {
  3. double d1 = Math.round(-5.6);
  4. double d2 = Math.round(5.6);
  5. System.out.println(d1);
  6. System.out.println(d2);
  7. }
  8. }