数字操作类

2020-12-17  本文已影响0人  曾梦想仗剑天涯

Math数学计算类

public class MathApi {
    public static void main(String[] args) {
        System.out.println(Math.abs(-10.1));
        System.out.println(Math.max(10.2, 20.1));
        System.out.println(Math.log(5));
        System.out.println(Math.round(15.4));
        System.out.println(Math.round(-15.51));
        System.out.println(Math.pow(10, 2));
    }
}
class MathUtil {
    private MathUtil() {}
    /** 
    * @Description: 实现数据的四舍五入操作
    * @Param: [num操作参数, scale保留位数]
    * @return: double 
    * @Author: mars_wu
    * @Date: 2020/12/17 下午3:21
    */ 
    public static double round(double num, int scale) {
        return Math.round(num * Math.pow(10, scale)) / Math.pow(10, scale);
    }
}
public class MathApi {
    public static void main(String[] args) {
        System.out.println(MathUtil.round(19.8623, 2));
    }
}

Random随机数生成类

import java.util.Random;
public class MathApi {
    public static void main(String[] args) {
        Random ran = new Random();
        System.out.println(ran.nextInt(100));
    }
}
import java.util.Arrays;
import java.util.Random;
public class MathApi {
    public static void main(String[] args) {
        int [] data = new int[7];
        int foot = 0;
        while (foot < 7) {
            Random ran = new Random();
            int num = ran.nextInt(38);
            if (isUse(num, data)) {
                data[foot++] = num;
            }
        }
        Arrays.sort(data);
        for (int x = 0; x < data.length; x++) {
            System.out.print(data[x] + "、");
        }
    }
    /** 
    * @Description: 判断传入参数是否为0以及数组中是否存在
    * @Param: [num随机生成值, temp目标数组]
    * @return: boolean true表示当前num可用,false表示当前值不可用
    * @Author: mars_wu
    * @Date: 2020/12/17 下午3:47
    */ 
    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;
    }
}

大数字处理类

此图来源于李兴华老师
import java.math.BigInteger;
public class MathApi {
    public static void main(String[] args) {
        BigInteger bigA = new BigInteger("283475918273948721983749");
        BigInteger bigB = new BigInteger("1982347983");
        System.out.println("加法" + bigA.add(bigB));
        System.out.println("减法" + bigA.subtract(bigB));
        System.out.println("乘法" + bigA.multiply(bigB));
        System.out.println("除法" + bigA.divide(bigB));
    }
}
import java.math.BigInteger;
public class MathApi {
    public static void main(String[] args) {
        BigInteger bigA = new BigInteger("283475918273948721983749");
        BigInteger bigB = new BigInteger("1982347983");
        BigInteger [] result = bigA.divideAndRemainder(bigB);
        System.out.println("商" + result[0] + "余" + result[1]);
    }
}
import java.math.BigDecimal;
import java.math.RoundingMode;
class MathUtil {
    private MathUtil() {}
    /** 
    * @Description: 实现数据的四舍五入操作
    * @Param: [num操作参数, scale保留位数]
    * @return: double 
    * @Author: mars_wu
    * @Date: 2020/12/17 下午3:21
    */ 
    public static double round(double num, int scale) {
        return new BigDecimal(num).divide(BigDecimal.valueOf(1.0), scale, RoundingMode.HALF_UP).doubleValue();
    }
}
public class MathApi {
    public static void main(String[] args) {
        System.out.println(MathUtil.round(19.6352, 2));
    }
}
上一篇下一篇

猜你喜欢

热点阅读