您可以使用BigInteger类。
BigInteger bi1 = new BigInteger("637824629384623845238423545642384");
BigInteger bi2 = new BigInteger("3039768898793547264523745379249934");
BigInteger bigSum = bi1.add(bi2);
BigInteger bigProduct = bi1.multiply(bi2);
System.out.println("Sum : " + bigSum);
System.out.println("Product : " + bigProduct);
输出:
总和:3677593528178171109762168924892318
产品 : 1938839471287900434078965247064711159607977007048190357000119602656
我应该提到BigDecimal ,与double相比,它非常适合金额计算。
BigDecimal bd = new BigDecimal("123234545.4767");
BigDecimal displayVal = bd.setScale(2, RoundingMode.HALF_EVEN);
NumberFormat usdFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(usdFormat.format(displayVal.doubleValue()));
输出:
123,234,545.48 美元