Math类
1、Math
类用于数学计算,提供了一系列的数学计算方法。Math类中的方法都是static方法,因为Math类没有普通属性。
2、在Math类中的四舍五入方法需要注意public static long round(double a);
范例:四舍五入
public class Demo {
public static void main(String[] args) throws Exception {
System.out.println(Math.round(15.5)); // 16
System.out.println(Math.round(-15.5)); // -15
System.out.println(Math.round(-15.51)); // -16
}
}
上述结果表明,负数四舍五入时,只有小数位大于0.5才进位,小于等于0.5不进位。
Random类
1、Random类主要功能是取得随机数
范例:生成10个小于00的随机数
public class Demo {
public static void main(String[] args) throws Exception {
Random rand = new Random();
for (int x = 0; x < 10; x++) {
System.out.println(rand.nextInt(100));
}
}
}
2、Random可以产生随机数,利用该特点实现一个36选7的功能。
思路:最大值为36,所以边界数值为37,且不能为0或者重复。
范例:36选7实现
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Random rand = new Random();
int[] data = new int[7];
int foot = 0;
while (foot < 7) {
// 生成一个小于37的随机数
int t = rand.nextInt(37);
if (!isRepeat(data, t)) {
data[foot++] = t;
}
}
java.util.Arrays.sort(data);
for (int aData : data) {
if (aData < 10) {
System.out.print("0" + aData + " ");
} else {
System.out.print(aData + " ");
}
}
}
/**
* 判断新生数据是否重复,且不能为0
* @param tmps 已经保存的数据组
* @param num 新生数据
* @return 重复则为true, 不重复则为false
*/
public static boolean isRepeat(int[] tmps, int num) {
if (num == 0) {
return true;
}
for (int tmp: tmps) {
if (tmp == num){
return true;
}
}
return false;
}
}
在开发中,经常使用随机数。
BigInterger类
当要操作的数据值很大,首先想到数据类型是double。假如计算结果范围超过了double,结果如下:
public class Demo {
public static void main(String[] args) throws Exception {
System.out.println(Double.MAX_VALUE * Double.MAX_VALUE); // Infinity
}
}
上述表明,结果不存在,超过了double的范围。
超过了double的范围,就无法使用double保存数据,只有用String类来保存。即进行数据很大的计算时只能将其变为String型,而后按位取出每一个字符保存的数据,进行手工计算。因此Java考虑到这种情况,专门提供了大数字的操作类,其中有BigInteger
、BigDecimal
。
BigInteger
类(是Number的子类)的构造方法:public BigInteger(Stringval)
,接收是String型数据。
import java.math.BigInteger;
public class Demo
public static void main(String[] args) throws Exception {
BigInteger bigA = new BigInteger("234112412124142141414");
BigInteger bigB = new BigInteger("23411241231212141414");
System.out.println("加法:" + (bigA.add(bigB)));
System.out.println("减法:" + (bigA.subtract(bigB)));
System.out.println("乘法:" + (bigA.multiply(bigB)));
System.out.println("除法:" + (bigA.divide(bigB)));
// 该数组只会有两个元素,第一个为商,第二个为余数
BigInteger result[] = bigA.divideAndRemainder(bigB);
System.out.println("商:" + result[0] + "余数" + result[1]);
}
}
BigDecimal类
1、BigInteger
不能保存小数,而BigDecimal
可以保存小数数据。BigDecimal
构造方法如下:
构造一:public BigDecimal(String val);
构造二:public BigDecimal(double val);
与BigInteger
一样,BigDecimal
也支持基础的数字计算。但使用BigDecimal
可以实现准确的四舍五入操作。虽然Math.round()
也可以实现四舍五入,但是它将小数四舍五入为整数。假设一家公司年收入为3.45678亿,按照Math.round()
结果为3亿,这不合理。
2、BigDecimal
中没有直接四舍五入的操作,但是可以利用除法计算实现:
除法操作:public BigDecimal divide(BigDecimal divisor, int scale, RoundingModeroundingMode)
(1)BigDecimal divisor
:被除数;
(2) int scale
:保留的小数位;
(3)RoundingMode roundingMode
:进位模式(public static final int ROUND_HALF_UP
)
**范例:**实现准确四舍五入
import java.math.BigDecimal;
class NewMath {
/**
* 精确四舍五入
* @param num 要进行四舍五入的数
* @param scale 要精确到多少位
* @return 四舍五入后的值
*/
public static double round(double num, int scale) {
BigDecimal dividend = BigDecimal.valueOf(num);
BigDecimal divisor = BigDecimal.valueOf(1);
return dividend.divide(divisor, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
public class Demo {
public static void main(String[] args) {
System.out.println(NewMath.round(3.1415926, 3));
}
}