S.Kraus
S.Kraus

Reputation: 389

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.

How can I overcome this problem? I need some kind of primitive that can hold it with floating point.

Thank you

Upvotes: 4

Views: 10077

Answers (4)

Will Ford
Will Ford

Reputation: 231

In java if you want accurate calculations for large numbers with fractions, you should use java.math.BigDecimal class. The integer counterpart is 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. Refer to this : http://en.wikipedia.org/wiki/Scientific_notation#E_notation

Upvotes: 6

Michael McGowan
Michael McGowan

Reputation: 6608

First, hopefully you recognize the issues with floating-point decimal representations.

17774132 is equivalent to 1.7774132E7; the "E7" means it is being multiplied by 10^7. If your issue is that you want it displayed differently, you can use a NumberFormat.

Note that 17774132 is actually an integer and well below the threshold of ~2.1 billion for the int primitive type. The long primitive type lets you go even higher if you are actually using integers.

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).

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60711

1.7774132E7 is exactly the same as 17774132. It's just displayed in scientific notation. A double is more than capable of holding this value. As others have said, though, use BigDecimal if you're worried about size or accuracy.

Upvotes: 1

SHiRKiT
SHiRKiT

Reputation: 1022

Remeber that means 1.7774132 * 10^7, so the value is represented by:

1.7774132 * 10000000

That's a big number, don't you think?

Java outputs by default on scientific notation if needed. Big numbers like that are expressed in scientific notation.

Upvotes: 2

Related Questions