Converting Double to String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ananth
    New Member
    • Nov 2006
    • 75

    #1

    Converting Double to String

    Hi All,
    I need to convert a Double value to String.The requirement is as follows

    Double do=123456789012 3456.12;
    String str=Double.toSt ring(do);

    when i execute it i am getting the values in exponential format.
    which is like: 1.2345678901234 56E15

    But i need the values to be converted exactly into String.I cannot use BigDecimals due to the requirements.

    The code is as follows.

    public class test1 {
    public static void main(String[] args) {
    double aDouble = 123456789012345 6.12;
    String aString=Double. toString(aDoubl e);
    System.out.prin tln(aString1);}
    }

    Please help me in this regard as it would be help ful for my project completion.

    Thanks in Advance!!!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ananth
    Hi All,
    I need to convert a Double value to String.The requirement is as follows

    Double do=123456789012 3456.12;
    String str=Double.toSt ring(do);

    when i execute it i am getting the values in exponential format.
    which is like: 1.2345678901234 56E15

    But i need the values to be converted exactly into String.I cannot use BigDecimals due to the requirements.

    The code is as follows.

    public class test1 {
    public static void main(String[] args) {
    double aDouble = 123456789012345 6.12;
    String aString=Double. toString(aDoubl e);
    System.out.prin tln(aString1);}
    }

    Please help me in this regard as it would be help ful for my project completion.

    Thanks in Advance!!!
    Have a look at what the DecimalFormat can do for you.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      A double uses 53 bits for its mantissa (or 'significand'). That implies that the
      order of magnitude between the leftmost (most significant) and rightmost bit
      (least significant) is 2^53 == 9.0071992547409 92E15 so at most 16 (a bit less)
      significant decimal digits can be stored in a double. Your value (which also is
      a denormalized value) tries to use more digits than that and you can never store
      it without a bit of loss of precision (those rightmost digits).

      kind regards,

      Jos

      Comment

      • Ozzi
        New Member
        • Oct 2007
        • 4

        #4
        It is not possible to receive such a BigDecimal in a Double.
        The easiest thing you can do is:

        BigDecimal myBigDecimal = new BigDecimal("123 4567890123456.1 2");
        System.out.prin tln(myBigDecima l.toPlainString ());

        Comment

        Working...