0

The title says it all: is there a way to store a really huge number using scientific notation? Something like BigInteger i = 1.9891*10^30?

For the record, I'm trying to store the mass of the Sun. In fact, in the end, I would like to be able to store the masses of several stars and planets, not to mention their radiuses, surface areas, volumes, etc. And I figure that storing the full values would use way to much memory.

Or do I need to craft my own class to handle scientific calculations and store such values?

EDIT

What I mean is, if I store the number 1.9891e30, how much disk space will be used? As much as if I stored 1989100000000000000000000000000, or less?

VACN
  • 75
  • 8
  • 1
    Duplicate of http://stackoverflow.com/questions/2944822/format-double-value-in-scientific-notation – CodeNewbie Sep 08 '14 at 09:37
  • @CodeNewbie I don't think it is a duplicate. I'm not only asking how to display a number in scientific, I'm asking if there is a way to store it, so as to take less disk space than the fully written value. I'll update the OP to add some clarification. – VACN Sep 08 '14 at 09:40
  • possible duplicate of [Is there a good Javascript BigDecimal library?](http://stackoverflow.com/questions/744099/is-there-a-good-javascript-bigdecimal-library) – Benny Bottema Sep 08 '14 at 09:48

3 Answers3

2

Try using E instead of *10^30 so:

BigInteger i = 1.9891E30?

I'm not sure if you won't have to use double for it though

To display it in scientific notation use printf() method and %g flag to display in scientific notation so something like:

System.out.printf("%g", i);
Lucas
  • 3,181
  • 4
  • 26
  • 45
1

Since you want to know if storing a value in scientific notation will help you save memory, I would like to point you to the JLS.

The double data type is a double-precision 64-bit IEEE 754 floating point.

So when you store the number 1989100000000000000000000000000, you will still utilize 8 bytes of data. You can represent it as 1.9891E30 (check this answer for code help) or as 1989100000000000000000000000000, but that will not change the storage size in any way.

Community
  • 1
  • 1
CodeNewbie
  • 2,003
  • 16
  • 29
0

Why don't you use double? You can store values up to around 10^4931.

For displaying value, you can use format (see here) or NumberFormat (see here).

R2B2
  • 1,541
  • 1
  • 12
  • 19