数字操作类

Math数学计算类

1.Math类

  • Math类的主要功能是:进行数学计算的操作类,提供基础的计算公式
    范例:
public class Demo {
    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));   //获取最大值: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
    }
}

自定义四舍五入功能

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 Demo {
    public static void main(String[] args) throws Exception {
        System.out.println(MathUtil.round(19.86273,2));     //19.86
    }
}

2.Random随机数生成类

  • java.util.Random类的主要功能是产生随机数
  • 产生一个不大于边界的随机正整数:public int nextInt​(int bound);
    范例:产生随机数
public class Demo {
    public static void main(String[] args) throws Exception {
        Random rand =new Random();
        for (int x = 0; x < 10; x++) {
            System.out.print(rand.nextInt(10)+"、");
        }
    }
}

3.大数字处理类

  • 解决这类问题,提供有两个大数字的操作类:BigInteger、BigDecimal。
1.BigInteger类构造:public BigInteger​(String val)2.BigDecimal类构造: public BigDecimal​(String val)

范例:使用BigInteger实现四则运算

public class Demo {
    public static void main(String[] args) throws Exception {
        BigInteger bigA=new BigInteger("234234234234234234");
        BigInteger bigB=new BigInteger("23423423");
        System.out.println("加法操作:"+bigA.add(bigB));    //234234234257657657
        System.out.println("减法操作:"+bigA.subtract(bigB));    //234234234210810811
        System.out.println("乘法操作:"+bigA.multiply(bigB));  //5486567549549549544062982
        System.out.println("除法操作:"+bigA.divide(bigB));      //10000000180
        
    }
}
  • 求余:public BigInteger[] divideAndRemainder​(BigInteger val);数组第一个元素为商,第二个为余数;
    范例:除法取余
public class Demo {
    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]);//商:100000000180、余数:18018094
    }
}
  • BigDecimal操作形式和BigInteger是非常类似的,都有基础的数学支持。

范例:使用BigDecimal计算

public class Demo {
    public static void main(String[] args) throws Exception {
        BigDecimal bigA = new BigDecimal("32890234890");
        BigDecimal bigB = new BigDecimal("1892039");
        System.out.println("加法计算:"+bigA.add(bigB));   //32892126929
        BigDecimal result[] = bigA.divideAndRemainder(bigB);
        System.out.println("除法计算:商:" + result[0] + "、余数:" + result[1]);//商:17383、余数:920953
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值