数字操作类
1:Math类介绍
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(Math.abs(-10.1)); // 10.1
System.out.println(Math.max(10.2, 20.3)); // 获取最大值
System.out.println(Math.log(5)); // 1.6094379124341003
System.out.println(Math.round(15.1)); // 15
System.out.println(Math.round(-15.5)); // -15
System.out.println(Math.round(-15.51)); // -16
System.out.println(Math.pow(10.2, 20.2)); // 2.364413713591828E20
}
}
10.1
20.3
1.6094379124341003
15
-15
-16
2.364413713591828E20
2:实现指定位数的保留
案例;自定义四舍五入的功能
class MathUtil {
private MathUtil() {}
/**
* 实现数据的四舍五入操作
* @param num 要进行四舍五入操作的数字
* @param scale 四舍五入保留的小数位数
* @return 四舍五入处理后的结果
*/
public static double round(double num,int scale) {
return Math.round(num * Math.pow(10, scale)) / Math.pow(10, scale) ;
}
}
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(MathUtil.round(19.86273, 2));
}
}
//19.86
Math类里面提供的基本上都是基础的数学公式,需要的时候自己重新组合。
3:Random类
import java.util.Random;
public class Test {
public static void main(String[] args) throws Exception {
Random rand = new Random() ;
for (int x = 0 ; x < 10 ; x ++) {
System.out.print(rand.nextInt(100) + "、");
}
}
}
//每次运行的结果是不同的
//32、83、19、0、20、69、81、34、62、40、
//85、89、87、96、13、54、54、12、0、0、
在国内有一款神奇的所谓的36选7的彩票,那么就可以利用Random实现随机生成彩票号。
import java.util.Arrays;
import java.util.Random;
public class JavaAPIDemo {
public static void main(String[] args) throws Exception {
int data [] = new int [7] ; // 开辟7个大小的空间
Random rand = new Random() ;
int foot = 0 ; // 操作data脚标
while(foot < 7) { // 选择7个数字
int num = rand.nextInt(37) ; // 生成一个数字
if (isUse(num,data)) { // 该数字现在可以使用
data[foot ++] = num ; // 保存数据
}
}
java.util.Arrays.sort(data);
for (int x = 0 ; x < data.length ; x ++) {
System.out.print(data[x] + "、");
}
}
/**
* 判断传入的数字是否为0以及是否在数组之中存在
* @param num 要判断的数字
* @param temp 已经存在的数据
* @return 如果该数字不是0并且可以使用返回true,否则返回false
*/
public static boolean isUse(int num,int temp[]) {
if (num == 0) {
return false ;
}
for (int x = 0 ; x < temp.length ; x ++) {
if (num == temp[x]) {
return false ;
}
}
return true ;
}
}
随机的操作都可以利用Random来处理。
4:大数据操作类
如果现在要想进行加法的计算,那么就需要逐位拆分,每一位自己计算,而后自己独立控制位处理,那么这样的开发难度是非常高的,提供有两个大数字的操作类:BigInterger,BigDecimal。
5:观察两个大数字操作类的构造方法
范例:使用BigInteger实现四则运算
import java.math.BigInteger;
public class Test {
public static void main(String[] args) throws Exception {
BigInteger bigA = new BigInteger("234234234234234234") ;
BigInteger bigB = new BigInteger("23423423") ;
System.out.println("加法操作:" + bigA.add(bigB));
System.out.println("减法操作:" + bigA.subtract(bigB));
System.out.println("乘法操作:" + bigA.multiply(bigB));
System.out.println("除法操作:" + bigA.divide(bigB));
}
}
加法操作:234234234257657657
减法操作:234234234210810811
乘法操作:5486567549549549544062982
除法操作:10000000180
6:观察性能问题
需要注意的是,虽然提供有大数字操作类,但是整体的操作之中还是需要考虑到一个性能问题。
import java.math.BigInteger;
public class JavaAPIDemo {
public static void main(String[] args) throws Exception {
BigInteger bigA = new BigInteger("234234234234234234") ;
System.out.println(bigA.pow(Integer.MAX_VALUE));
}
}
*7:求余数 *
此时的计算过程是非常缓慢的,所以任何的电脑都是有极限的,既然在进行数学除法的时候有可能无法进行整数处理,那么可以使用其他的除法计算来求出余数:
import java.math.BigInteger;
public class Test {
public static void main(String[] args) throws Exception {
BigInteger bigA = new BigInteger("234234234234234234") ;
BigInteger bigB = new BigInteger("23423423") ;
BigInteger result [] = bigA.divideAndRemainder(bigB) ;
System.out.println("商:" + result[0] + "、余数:" + result[1]);
}
}
商:10000000180、余数:18018094
8:使用BigDecimal操作形式和 BigInteger计算
import java.math.BigInteger;
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) throws Exception {
BigDecimal bigA = new BigDecimal("32890234890") ;
BigDecimal bigB = new BigDecimal("1892039") ;
System.out.println("加法计算:" + bigA.add(bigB));
BigDecimal result [] = bigA.divideAndRemainder(bigB) ;
System.out.println("除法计算,商:" + result[0] + "、余数:" + result[1]);
}
}
加法计算:32892126929
除法计算,商:17383、余数:920953
9:使用BigDecimal实现四舍五入
但是在使用BigDecimal的时候有一个数据进位问题,在这个类里面定义有如下的除法运算:
范例:使用BigDecimal实现四射五入处理
import java.math.BigDecimal;
import java.math.RoundingMode;
class MathUtil {
private MathUtil() {}
/**
* 实现数据的四舍五入操作
* @param num 要进行四舍五入操作的数字
* @param scale 四舍五入保留的小数位数
* @return 四舍五入处理后的结果
*/
public static double round(double num,int scale) {
return new BigDecimal(num).divide(new BigDecimal(1.0), scale, RoundingMode.HALF_UP).doubleValue();
}
}
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(MathUtil.round(19.6352, 2));
}
}
//19.64
总结:Math的处理由于使用的都是基本数据类型,所以性能一定要高于大数字处理类。