Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing large decimal numbers in Java

Tags:

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

like image 557
S.Kraus Avatar asked Jan 18 '23 13:01

S.Kraus


2 Answers

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

like image 63
Will Ford Avatar answered Jan 21 '23 04:01

Will Ford


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.

like image 20
SHiRKiT Avatar answered Jan 21 '23 04:01

SHiRKiT