Results 1 to 4 of 4
  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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. #2
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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!

Similar Threads

  1. Replies: 17
    Last Post: 09-15-2012, 03:50 AM
  2. Replies: 1
    Last Post: 08-30-2012, 12:07 AM
  3. Replies: 3
    Last Post: 02-10-2012, 06:26 PM
  4. How to remove text up to second full stop in a string
    By sundharavaradhan in forum New To Java
    Replies: 5
    Last Post: 12-10-2011, 03:03 PM
  5. short [] <-> double[] conversion query
    By KittenFace in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 07:30 PM

Posting Permissions

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