Java大数值

BigInteger类实现了任意精度的整数运算,BigDecimal类实现了任意精度的浮点数运算

使用静态类valueOf方法可以将普通的数值转换为大数值:

BigInteger a = BigInteger.valueOf(60);

示例代码

package com.company;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigInteger a = BigInteger.valueOf(490);
        BigInteger b = BigInteger.valueOf(60);
        System.out.println(a.add(b));//加
        System.out.println(a.subtract(b));;//减
        System.out.println(a.multiply(b));;//乘
        System.out.println(a.divide(b));;//除
        System.out.println(a.mod(b));;//除

        BigDecimal c = BigDecimal.valueOf(490);
        BigDecimal d = BigDecimal.valueOf(60);
        System.out.println(c.add(d));//加
        System.out.println(c.subtract(d));;//减
        System.out.println(c.multiply(d));;//乘
        System.out.println(c.divide(d, 20,RoundingMode.HALF_UP));;//除
    }
}

posted @ 2020-03-23 23:31  xl4ng  阅读(207)  评论(0编辑  收藏  举报