Converting double to string in 7e100 format

Discussion in 'Java' started by Pudu, Jun 18, 2004.

  1. Pudu

    Pudu Guest

    I have a double value 17435340000000000000. I want to convert it to
    1.743534e20 and it should be a string value. Any help would be
    appreciated.
     
    Pudu, Jun 18, 2004
    #1
    1. Advertising

  2. Pudu

    Roedy Green Guest

    On 18 Jun 2004 15:42:36 -0700, (Pudu) wrote or
    quoted :

    >I have a double value 17435340000000000000. I want to convert it to
    >1.743534e20 and it should be a string value. Any help would be
    >appreciated.


    see http://mindprod.com/jgloss/converter.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 18, 2004
    #2
    1. Advertising

  3. Pudu

    Liz Guest

    in 1.5 sprintf()


    "Pudu" <> wrote in message
    news:...
    > I have a double value 17435340000000000000. I want to convert it to
    > 1.743534e20 and it should be a string value. Any help would be
    > appreciated.
     
    Liz, Jun 19, 2004
    #3
  4. Pudu

    Guest

    Pudu wrote:

    > I have a double value 17435340000000000000. I want to convert it to
    > 1.743534e20 and it should be a string value. Any help would be
    > appreciated.


    Peace be unto you.

    import java.text.DecimalFormat;
    class TestingAndQualityAssuranceDepartment2
    {
    public static void main(String [] args)
    {
    double x = 17435340000000000000d;
    DecimalFormat df = new DecimalFormat("0.000000E00");
    System.out.println(df.format(x).toLowerCase());
    }
    }

    >java TestingAndQualityAssuranceDepartment2

    1.743534e19
    >Exit code: 0



    Have a good day.
     
    , Jun 19, 2004
    #4
  5. Pudu

    Guest

    Liz wrote:

    > in 1.5 sprintf()


    Peace.



    import java.text.DecimalFormat;
    import java.util.Locale;
    import java.util.Formatter;
    import java.text.MessageFormat;
    class TestingAndQualityAssuranceDepartment3
    {
    public static void main(String [] args)
    {
    double x = 0.0000000000001743534d;
    System.out.printf("%1$+e\n", x);
    System.out.println(Double.toString(x).toLowerCase());
    StringBuilder sb = new StringBuilder(); // Appendable
    Formatter formatter = new Formatter(sb, Locale.CANADA);
    formatter.format("%e", x);
    System.out.printf("%s",sb.toString());
    Object [] obj = {new Double(x)};
    MessageFormat mf = new MessageFormat("\n{0,number,0.000000E00}");
    System.out.printf("%s\n", mf.format(obj));
    DecimalFormat df = new DecimalFormat("0.000000E00");
    System.out.println(df.format(x).toLowerCase());
    }
    }

    "C:\Program Files\Java\jdk1.5.0\bin\javac" -source 1.5
    TestingAndQualityAssuranceDepartment3.java
    "C:\Program Files\Java\jdk1.5.0\bin\java.exe" -version:1.5
    TestingAndQualityAssuranceDepartment3
    +1.743534e-13
    1.743534e-13
    1.743534e-13
    1.743534E-13
    1.743534e-13



    %[argument_index$][flags][width]conversion
    You can get away with just %e - e being the conversion;
    or %1$e - $1 being the argument_index
    or %1$+e - + being a flag to indicating The result will always include
    a sign
    sprintf?

    Have a good day.
     
    , Jun 19, 2004
    #5
  6. Pudu

    Chris Smith Guest

    > "Pudu" <> wrote in message
    > news:...
    > > I have a double value 17435340000000000000. I want to convert it to
    > > 1.743534e20 and it should be a string value. Any help would be
    > > appreciated.


    Liz wrote:
    > in 1.5 sprintf()


    Perhaps it's time to start thinking about what's a good time to use
    PrintWriter's new printf methods. I see this as a definite border case,
    because the goal is a single String conversion of a data type. If the
    goal were to print lines formatted in a specific way while plugging in
    some set of multiple values, then the situation would be different.

    Is there a good reason to do this with PrintWriter.printf, instead of
    with DecimalFormat?

    Incidentally, I don't see a sprintf per se in the 1.5 API docs. Am I
    missing it? If it's PrintWriter.printf you're referring to, then this
    involves even more added complexity of creating a StringWriter to wrap
    in the PrintWriter, and then retrieving the result.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jun 20, 2004
    #6
  7. Pudu

    Liz Guest

    "Chris Smith" <> wrote in message
    news:4.net...
    > > "Pudu" <> wrote in message
    > > news:...
    > > > I have a double value 17435340000000000000. I want to convert it to
    > > > 1.743534e20 and it should be a string value. Any help would be
    > > > appreciated.

    >
    > Liz wrote:
    > > in 1.5 sprintf()

    >
    > Perhaps it's time to start thinking about what's a good time to use
    > PrintWriter's new printf methods. I see this as a definite border case,
    > because the goal is a single String conversion of a data type. If the
    > goal were to print lines formatted in a specific way while plugging in
    > some set of multiple values, then the situation would be different.
    >
    > Is there a good reason to do this with PrintWriter.printf, instead of
    > with DecimalFormat?


    It's easy and it's free.

    >
    > Incidentally, I don't see a sprintf per se in the 1.5 API docs. Am I
    > missing it? If it's PrintWriter.printf you're referring to, then this
    > involves even more added complexity of creating a StringWriter to wrap
    > in the PrintWriter, and then retrieving the result.


    I didn't look really, since I'm not going to use 1.5 until it is official.
    (i.e. not one of those greek names like hans beta)
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
     
    Liz, Jun 22, 2004
    #7
  8. Pudu

    Chris Smith Guest

    Liz wrote:
    > > Is there a good reason to do this with PrintWriter.printf, instead of
    > > with DecimalFormat?

    >
    > It's easy and it's free.


    And yet DecimalFormat provides the same service, and is equally easy (as
    far as I can tell) and certainly equally free...

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jun 22, 2004
    #8
  9. Pudu

    Dale King Guest

    Hello, Liz!
    You wrote:

    > in 1.5 sprintf()


    1.5 will indeed have a printf/sprintf type of functionality, but
    it is not actually called sprintf. IIRC there is a class called
    formatter that provides the printf functionality and String will
    gain a format method to do the equivalent of sprintf.
    --
    Dale King
    My Blog: http://daleking.homedns.org/Blog
     
    Dale King, Apr 15, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,174
    Janaka
    Feb 18, 2004
  2. D. Shane Fowlkes
    Replies:
    2
    Views:
    576
    D. Shane Fowlkes
    Dec 16, 2004
  3. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    3
    Views:
    15,182
    NullBock
    Jan 23, 2006
  4. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  5. Fei Liu
    Replies:
    21
    Views:
    2,383
    John Bokma
    Dec 16, 2006
Loading...

Share This Page