# Thread: Full conversion of double to string

1. Member
Join Date
Feb 2017
Posts
24
Rep Power
0

## Full conversion of double to string

Here is a code snippet:

Java Code:
```double result;
String bigNumber;

result = Math.pow(exp,100);
System.out.println("2^100 = " + result);

DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString = decimalFormat.format(result);
System.out.println("Number as string means result = " + numberAsString);```
My output is:
2^100 = 1.2676506002282294E30
Number as string means result = 1267650600228229400000000000000

I really need all 31 digits of the answer. I have tried to experiment with other means of getting java to convert this number into the number list I need (my actual need is for a lot bigger number, but I am proving the method on a more tractable problem).

Is there any way to get the exact number, converting from scientific notation to decimal? From the examples I have seen looking on the web, unless I can find a way to grab the truncated digits, using DecimalFormat to go from string to double is not going to work.

Do I need, given the size of the number, to parse the number into manageable pieces, convert to strings, and move forward from there?

Or am I just missing the boat entirely?

Thanks...

2. Member
Join Date
Feb 2017
Posts
24
Rep Power
0

## Re: Full conversion of double to string

I think I found a solution, but I think I still need some advice.

I used the following code:

Java Code:
```double result;
int k;

result = 1234;
String bigNumber = Double.toString(result);
System.out.println("Character conversion of 1234 = " + bigNumber);
char[] charNumber = bigNumber.toCharArray();

for (k=0;k<4;k++) {
System.out.println("Character " + k + " of the string is " + charNumber[k]);
} // end of for```
I can use this to "chop" the number into reasonable pieces. But this is rather awkward.
Is this the only way to access all the numbers of a very big number?

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Full conversion of double to string

The problem is that even for doubles and longs they are only 64 bits wide. For doubles, only 53 bits of the actual number is used for the fraction. The rest are for the sign bit and exponent. Check IEEE754 on Wiki for detailed information. However, what you really want to look at is BigDecimal (for floating point) and BigInteger for (integer) classes. Both of these offer arbitrary precision and will include all the digits. They are in the java.math package.

Regards,
Jim

4. Member
Join Date
Feb 2017
Posts
24
Rep Power
0

## Re: Full conversion of double to string

Jim,

Sorry for the delay.
BigDecimal worked perfectly. Thank you.

Holiday activities got in the way of a timely reply.

Happy holidays to you!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•