Java数字操作类

导语

数学操作类的使用,其中BigInteger、BigDecimal可以操作大数字。最下面是重点,实现准确的四舍五入操作。

主要内容

  • Math类
  • Random类
  • 大数字操作类

具体内容

Math类

Math就是一个专门进行数学计算的操作类,里面提供了一系列的数学计算方法。
在Math类里面提供的一切方法都是static型的方法,因为Math类里面没有普通属性。

Math有一个方法要引起注意:
四舍五入:public static long round(double a)。
范例:观察四舍五入

public class TestDemo {
    public static void main(String args[]) {
        System.out.println(Math.round(15.5));
        System.out.println(Math.round(-15.5));
        System.out.println(Math.round(-15.51));
    }
}

输出结果

16
-15
-16

如果进行负数四舍五入的时候,操作的数据小数位大于0.5才进位,小于等于0.5不进位。

Random类

这个类的主要功能是取得随机数的操作类。

范例:产生10个不大于100的正整数(0~99)

public class TestDemo {
    public static void main(String args[]) {
        Random rand = new Random();
        for(int i = 0; i < 10; i++) {
            System.out.print(rand.nextInt(100) + "、");
        }
    }
}

输出结果可能为

53、31、77、51、68、23、59、63、27、93、

大整数操作类:BigInteger

如果说现在要操作的数据值很大,那么首先想到的应该是double,那么如果说现在计算的结果超过了double会怎样。

public class TestDemo {
    public static void main(String args[]) {
        System.out.println(Doublic.MAX_VALUE * Doublic.MAX_VALUE);
    }
}

输出结果

Infinity

现在发现此时的计算结果并不存在,因为已经超过了double的范畴。
面试题:请问当前假设有两个很大的数字要进行数学计算(超过了doulble范围),你该怎么做?
如果真的超过了double的范围,那么肯定无法使用double进行保存,只有String型才能保存。如果真的数据很大的数字要进行数学计算,只能够将其变为String型,而后按位取出每一个字符保存的数据,进行手工的计算。
所以在Java里面考虑到了此类情况,专门提供了大数字的操作类,其中就有BigInteger、BigDecimal两种。

BigInteger类的构造方法:public BigInteger(String val),它接收的是String型。

public class TestDemo {
    public static void main(String args[]) {
        BigInteger bigA = new BigInteger("124123412141234");
        BigInteger bigB = new BigInteger("987654345");
        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]);
    }
}

输出结果

加法操作:124124399795579
减法操作:124122424486889
乘法操作:122591027317515513761730
除法操作:125674
商:125674  余数:939987704

在Java里面虽然提供了大数字的操作类,但是很多的时候,我们使用的项目开发可能对于数字要求会更加的敏感,这个时候Java本身所提供的数字类是帮助不大的。
这个时候应该去找第三方的包。

大浮点数操作类:BigDecimal

BigInteger 不能够保存小数,而BigDecimal可以保存小数数据。在BigDecimal里提供有如下几种构造:
- 构造一:public BigDecimal(String val)。
- 构造二:public BigDecimal(double val)。

与BigInteger一样,BigDecimal本身也支持基础的数学计算,可是使用BigDecimal还有一个非常重要的目的,就是可以使用它来实现准确的四舍五入操作。

之前使用的Math.round()实现四舍五入操作,但是这种操作有一个问题,所有的小数位都四舍五入了。
遗憾的是BigDecimal类里面没有直接提供有四舍五入的操作支持,可是可以利用除法实现:
- 除法操作:public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)。
- BigDecimal divisor:被除数。
- int scale:保留的小数位。
- int roundingMode:进位模式(public static final int ROUND_HALF_UP)。

范例:实现准确的四舍五入

public class MyMath {
    /**
     * 实现准确位数的四舍五入操作
     * @param num 要进行四舍五入操作的数字
     * @param scale 要保留的小数位
     * @return 处理后的四舍五入数据
     */
    public static double round(double num, int scale) {
        BigDecimal bigA = new BigDecimal(num);
        BigDecimal bigB = new BigDecimal(1);
        return bigA.divide(bigB, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

public class TestDemo {
    public static void main(String args[]) {
        System.out.println(MyMath.round(19.783231231, 2));
        System.out.println(MyMath.round(-15.5, 0));
        System.out.println(MyMath.round(15.5, 0));
    }
}

输出结果

19.78
-16
16

此类操作的功能在日后的开发之中一定要会使用,属于工具类的支持范畴。

总结

  • Math类重要要清楚round()方法的坑。
  • Random类生成随机数。
  • 如果数据量大就使用BigInteger或BigDecimal,这两个类是Number的子类。

进入我的CSDN戳这里(我的博客导航)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值