Results 1 to 4 of 4
- 12-16-2017, 07:24 AM #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);
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...
- 12-16-2017, 02:41 PM #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
Is this the only way to access all the numbers of a very big number?
- 12-16-2017, 06:07 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 12-21-2017, 06:52 PM #4
Member
- Join Date
- Feb 2017
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?
By jayjay5 in forum New To JavaReplies: 17Last Post: 09-15-2012, 04:50 AM -
double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?
By jayjay5 in forum IntroductionsReplies: 1Last Post: 08-30-2012, 01:07 AM -
Dealing with floating-point in double and String and convert it to String
By emad7105 in forum New To JavaReplies: 3Last Post: 02-10-2012, 07:26 PM -
How to remove text up to second full stop in a string
By sundharavaradhan in forum New To JavaReplies: 5Last Post: 12-10-2011, 04:03 PM -
short [] <-> double[] conversion query
By KittenFace in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 PM
Bookmarks