存储比Java中的long类型长的数字 [英] Store a number that is longer than type long in Java

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

问题描述

如何在Java中存储比长类型(MAX:9223372036854775807)长的数字?

How can I store a number that is longer than the long type (MAX: 9223372036854775807) in Java?

例如,编号为9223372036854775820.

For example the number 9223372036854775820.

谢谢.

推荐答案

如果使用长整数,则使用 BigInteger ;如果使用浮点数,则使用 BigDecimal . BigInteger 可以随心所欲,直到没有足够的RAM.

Use BigInteger if you work with a long and use BigDecimal if you work with floatingpoint numbers. The BigInteger can be as big as you want, till there is not enough RAM.

示例:

    BigInteger bd = new BigInteger("922337203685477582012312321");
    System.out.println(bd.multiply(new BigInteger("15")));
    System.out.println(bd);

输出:

13835058055282163730184684815
922337203685477582012312321

但是必须使用 BigInteger 方法进行计算,在该示例中,您看到 BigInteger 是不可变的.

But have to use the BigInteger methods to do calculations and in the example you see that BigInteger is immutable.

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

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