Is there a way to diplay a float value 22.19999999998 as 22.2?

Discussion in 'Java' started by Harish Madhavan K, May 7, 2004.

  1. Hi,
    The value retrned from the server is 22.1999999998
    How do I display/pirntln this as 22.2 in a gui/console?
    Thanks
    Harish
     
    Harish Madhavan K, May 7, 2004
    #1
    1. Advertising

  2. Forget about this, java.text.NumberFormat is the answer.

    "Harish Madhavan K" <> wrote in message
    news:c7g83b$...
    > Hi,
    > The value retrned from the server is 22.1999999998
    > How do I display/pirntln this as 22.2 in a gui/console?
    > Thanks
    > Harish
    >
    >
     
    Harish Madhavan K, May 7, 2004
    #2
    1. Advertising

  3. Harish Madhavan K wrote:

    >Hi,
    > The value retrned from the server is 22.1999999998
    > How do I display/pirntln this as 22.2 in a gui/console?
    >Thanks
    >Harish
    >

    Should be possible with class java.text.DecimalFormat (although much
    more can be done with it)
    Read
    http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
    for examples.

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, May 7, 2004
    #3
  4. Harish Madhavan K

    Roedy Green Guest

    Roedy Green, May 7, 2004
    #4
  5. Harish Madhavan K

    Mason Bryant Guest

    Mason Bryant, May 7, 2004
    #5
  6. "Harish Madhavan K" <> wrote in message news:<c7g83b$>...
    > Hi,
    > The value retrned from the server is 22.1999999998
    > How do I display/pirntln this as 22.2 in a gui/console?
    > Thanks
    > Harish


    You could use a NumberFormat to display the value to one decimal place.

    fmt = NumberFormat.getNumberInstance();
    fmt.setMaximumFractionDigits(1);

    double myValue = 22.1999999998;
    String valStr = fmt.format(myValue);
     
    Karl von Laudermann, May 7, 2004
    #6
    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. m3ckon

    Database Diplay Format

    m3ckon, Aug 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    =?Utf-8?B?S2FyaW4=?=
    Aug 18, 2004
  2. daniel
    Replies:
    1
    Views:
    2,101
    Tom St Denis
    Jun 24, 2003
  3. bd
    Replies:
    0
    Views:
    635
  4. lucky
    Replies:
    15
    Views:
    1,704
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,561
    James Kanze
    Oct 8, 2009
Loading...

Share This Page