简体   繁体   English

在Java中存储大的十进制数

[英]Storing large decimal numbers in Java

I need to store 17774132 in a double format, but it seems that double is to small since I get 1.7774132E7. 我需要以双重格式存储17774132,但似乎双倍是小的,因为我得到1.7774132E7。

How can I overcome this problem? 我怎样才能克服这个问题? I need some kind of primitive that can hold it with floating point. 我需要一些可以用浮点来保存它的原语。

Thank you 谢谢

In java if you want accurate calculations for large numbers with fractions, you should use java.math.BigDecimal class. 在java中,如果要对包含分数的大数进行精确计算,则应使用java.math.BigDecimal类。 The integer counterpart is java.math.BigInteger. 整数对应的是java.math.BigInteger。

Also I think double can accomodate 17774132, it's just showing the value in something called as "E Notation" which a Scientific notation to denote numbers. 此外,我认为双重可以容纳17774132,它只是显示称为“E符号”的东西的值,科学符号表示数字。 Refer to this : http://en.wikipedia.org/wiki/Scientific_notation#E_notation 请参阅: http//en.wikipedia.org/wiki/Scientific_notation#E_notation

Remeber that means 1.7774132 * 10^7, so the value is represented by: 记住这意味着1.7774132 * 10 ^ 7,因此该值表示为:

1.7774132 * 10000000 1.7774132 * 10000000

That's a big number, don't you think? 这是一个很大的数字,你不觉得吗?

Java outputs by default on scientific notation if needed. 如果需要,Java默认使用科学计数法输出。 Big numbers like that are expressed in scientific notation. 像这样的大数字用科学记数法表达。

1.7774132E7 is exactly the same as 17774132. It's just displayed in scientific notation . 1.7774132E7与17774132完全相同。它只是以科学记数法显示。 A double is more than capable of holding this value. double超过能够保持这个值。 As others have said, though, use BigDecimal if you're worried about size or accuracy. 正如其他人所说,如果您担心尺寸或准确性,请使用BigDecimal

First, hopefully you recognize the issues with floating-point decimal representations. 首先,希望你认识到问题与浮点十进制表示。

17774132 is equivalent to 1.7774132E7 ; 17774132相当于1.7774132E7 ; the "E7" means it is being multiplied by 10^7 . “E7”表示它乘以10^7 If your issue is that you want it displayed differently, you can use a NumberFormat . 如果您的问题是希望不同方式显示 ,则可以使用NumberFormat

Note that 17774132 is actually an integer and well below the threshold of ~2.1 billion for the int primitive type. 请注意, 17774132实际上是一个整数,远低于int基本类型的约21亿的阈值。 The long primitive type lets you go even higher if you are actually using integers. 如果您实际使用整数,则long基本类型可以让您更高。

If you want to represent the number differently and it is not an integer, you can try BigDecimal (or BigInteger for legitimate integers too big for a long). 如果你想以不同的方式表示数字并且它不是一个整数,你可以尝试BigDecimal (或BigInteger用于合法的整数太长时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM