Java中的大数字 [英] Large Numbers in Java

查看:210
本文介绍了Java中的大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Java中使用极大数字进行计算?

How would I go about doing calculations with extremely large numbers in Java?

我试过 long 但是最大值为9223372036854775807,并且当使用整数时它没有保存足够的数字因此对我所需要的不够准确。

I have tried long but that maxes out at 9223372036854775807, and when using an integer it does not save enough digits and therefore is not accurate enough for what I need.

还有这个吗?

推荐答案

你可以使用 BigInteger 整数类, BigDecimal 表示带小数位数的数字。这两个类都在 java.math 包中定义。

You can use the BigInteger class for integers and BigDecimal for numbers with decimal digits. Both classes are defined in java.math package.

示例:

BigInteger reallyBig = new BigInteger("1234567890123456890");
BigInteger notSoBig = new BigInteger("2743561234");
reallyBig = reallyBig.add(notSoBig);

这篇关于Java中的大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆