Math 类是数学操作类,提供了一系列的数学操作方法,包括求绝对值、三角函数等等。在 Math 类里面提供的方法都是静态方法,都可以由类名直接调用。
方法名 | 描述 |
---|---|
public static double sqrt(double a) | 求平方根 |
public static int max(int a, int b) | 求两个数中比较大的那个数 |
public static int min(int a, int b) | 求两个数中比较小的那个数 |
public static double pow(double a, double b) | 求 a 数据的 b 次方的值 |
public static int round(float a) | 求数字 a 的四舍五入法的值 |
package com.haicoder.net.math;
public class MathTest {
public static void main(String[] args) {
System.out.println("嗨客网(www.haicoder.net)");
System.out.println("求平方根:" + Math.sqrt(16.0));
System.out.println("求两个数的最大值:" + Math.max(100, 200));
System.out.println("求两个数的最小值:" + Math.min(100, 200));
System.out.println("3 的 4 次方:" + Math.pow(3.0, 4.0));
System.out.println("四舍五入:" + Math.round(55.9));
}
}
运行结果如下:
Random 是随机产生一个数字,它可以指定随机数产生的范围。
方法名 | 描述 |
---|---|
public boolean nextBoolean() | 随机生成 boolean 值 |
public double nextDouble() | 随机生成double值 |
public float nextFloat() | 随机生成 float 值 |
public int nextInt() | 随机生成 int 值 |
public int nextInt(int n) | 随机生成给定的最大值的int值 |
public long nextLong() | 随机生成long值 |
package com.haicoder.net.math;
import java.util.Random;
public class MathTest {
public static void main(String[] args) {
System.out.println("嗨客网(www.haicoder.net)");
System.out.println("生成的 boolean 值 : " + new Random().nextBoolean());
System.out.println("生成的 double 值 : " + new Random().nextDouble());
System.out.println("生成的 float 值 : " + new Random().nextFloat());
System.out.println("生成的 int 值 : " + new Random().nextInt());
System.out.println("生成的 long 值 : " + new Random().nextLong());
System.out.println("生成的 小于参数的 int 值 : " + new Random().nextInt(10));
}
}
运行结果如下
float 和 double 类型可以定义小数相关的数据,但是它们会有精度的丢失,Bigdecimal 的精确度比较高,一般项目中涉及到钱相关的计算,都会用 Bigdecimal 来定义。它也可以对大数进行定义。
方法名 | 描述 |
---|---|
public BigDecimal(double value) | 构造方法,将 double 数据类型转换成 BigDecimal 类型 |
public BigDecimal(int value) | 构造方法,将 int 数据类型转换成 BigDecimal 类型 |
public BigDecimal(String value) | 构造方法,将 String 数据类型转换成 BigDecimal 类型 |
public BigDecimal add(BigDecimal value) | 普通方法,将两个BigDecimal 数据类型的数据进行相加 |
public BigDecimal subtract(BigDecimal value) | 普通方法,将两个BigDecimal 数据类型的数据进行相减 |
public BigDecimal multiply(BigDecimal value) | 普通方法,将两个BigDecimal 数据类型的数据进行相乘 |
public BigDecimal divide(BigDecimal value) | 普通方法,将两个BigDecimal 数据类型的数据进行相除 |
package com.haicoder.net.math;
import java.math.BigDecimal;
public class MathTest {
public static void main(String[] args) {
System.out.println("嗨客网(www.haicoder.net)");
BigDecimal doubleValue = new BigDecimal(20.00);
BigDecimal intValue = new BigDecimal(12);
BigDecimal floatValue = new BigDecimal(12.0f);
BigDecimal stringValue = new BigDecimal("10");
System.out.println("doubleValue 值 : " + doubleValue);
System.out.println("intValue 值 : " + intValue);
System.out.println("floatValue 值 : " + floatValue);
System.out.println("stringValue 值 : " + stringValue);
System.out.println("两个数相加 doubleValue + intValue 值 : " + doubleValue.add(intValue));
System.out.println("两个数相减 doubleValue - intValue 的值 : " + doubleValue.subtract(intValue));
System.out.println("两个数相乘 doubleValue * stringValue 的值 : " + doubleValue.multiply(stringValue));
System.out.println("两个数相除 doubleValue / intValue 的值 : " + doubleValue.divide(stringValue));
}
}
运行结果如下:
在 Java 语言 里对数学常用的类有 Math、Random、和 Bigdecimal。其实还有很多类,我们只列举出了常用的数学类。这些类提供了一些公共方法,我们只要熟练使用即可。