Round a Double

Discussion in 'Java' started by Guest, Oct 22, 2004.

  1. Guest

    Guest Guest

    e.g.

    Double d = 12.34567;
    I want this String: "12.35" and this Double: 12.35

    Double d = 12;
    I want this String: "12.00" and this Double: 12


    How?
     
    Guest, Oct 22, 2004
    #1
    1. Advertising

  2. Guest

    Paul Tomblin Guest

    In a previous article, "<- Chameleon ->" <> said:
    >Double d = 12.34567;
    >I want this String: "12.35" and this Double: 12.35
    >
    >Double d = 12;
    >I want this String: "12.00" and this Double: 12


    For the double, you could use the time honoured

    d = (int)(d * 100 + 0.5) / 100.0;

    which is what we've been using since the days when C wasn't ISO/ANSI-fied.

    For the string, look up java.text.DecimalFormat, which can probably do
    what you want.


    --
    Paul Tomblin <> http://xcski.com/blogs/pt/
    When you need a helpline for breakfast cereals, it's about time to think
    about tearing down civilisation and giving the ants a go.
    -- Chris King
     
    Paul Tomblin, Oct 22, 2004
    #2
    1. Advertising

  3. Guest

    Chris Smith Guest

    In article <clb7b4$9rs$>,
    says...
    > Double d = 12.34567;
    > I want this String: "12.35" and this Double: 12.35
    >
    > Double d = 12;
    > I want this String: "12.00" and this Double: 12


    To get the strings, see java.text.DecimalFormat. Specifically:

    DecimalFormat fmt = new DecimalFormat();
    fmt.setMinimumFractionDigits(2);
    fmt.setMaximumFractionDigits(2);
    String str = fmt.format(12.34567);

    Getting a Double is more difficult, and it's unclear what you want.
    Doubles in Java are not stored in BCD, but rather as binary floating-
    point numbers, so it makes no sense to talk about truncating them to a
    certain number of decimal digits. Any given two decimal place decimal
    number may or may not even be exactly representable as a float; it may
    actually be a repeating decimal instead.

    However, while there is not guaranteed to be a double that's exactly
    equal to 12.35, there *is* guaranteed to be a double that will be
    converted to "12.35" by methods such as Double.toString, String.valueOf,
    and so forth. The easiest way to get that is to do the String
    conversion above, and then use Double.parseDouble to get a double back
    out (or Double.valueOf if you need a Double instead). Just be aware
    that the value of the result may not be exactly 2.35; it will be the
    double value nearest to 2.35 that can be exactly represented in binary
    floating point.

    (I'm using 2.35 merely as an example above, so when I say "may not" or
    "may", I certainly *don't* mean that the behavior is undefined. Rather
    I mean that it's perfectly well defined for a given value; but changing
    2.35 to a different value could change those facts.)

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Oct 22, 2004
    #3
  4. Guest

    Carl Howells Guest

    Chris Smith wrote:

    > (I'm using 2.35 merely as an example above, so when I say "may not" or
    > "may", I certainly *don't* mean that the behavior is undefined. Rather
    > I mean that it's perfectly well defined for a given value; but changing
    > 2.35 to a different value could change those facts.)


    2.35 turns out to be one of those values that isn't exactly expressible
    in binary, so that disclaimer wasn't strictly necessary.

    Quickest way to see this: .35 = .25 + .1
    And while .25 decimal is obviously exactly representable in in binary
    (.01), .1 decimal is the classic value which isn't exactly representable
    in binary, so their sum clearly can't be represented exactly in binary.
     
    Carl Howells, Oct 22, 2004
    #4
  5. Guest

    marcus Guest

    If I need Strings of a particular number of decimal places I usually
    work in ints at the order of magnitude I need. 16.35 = 1635, with a
    routine that takes the int and returns a string, modified with
    placeholders if necessessary. Fast and neat, and no rounding.

    -- clh

    <- Chameleon -> wrote:
    > e.g.
    >
    > Double d = 12.34567;
    > I want this String: "12.35" and this Double: 12.35
    >
    > Double d = 12;
    > I want this String: "12.00" and this Double: 12
    >
    >
    > How?
     
    marcus, Oct 22, 2004
    #5
  6. Guest

    thakre_iit

    Joined:
    May 28, 2008
    Messages:
    3
    DecimalFormat fmt = new DecimalFormat();
    fmt.setMinimumFractionDigits(0);
    fmt.setMaximumFractionDigits(0);
    String str = fmt.format(12.64567);
    System.out.println("...."+str);
     
    thakre_iit, May 28, 2008
    #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. Sydex
    Replies:
    12
    Views:
    6,659
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    0
    Views:
    1,134
  3. John H.

    Re: round double to float

    John H., Apr 27, 2010, in forum: C++
    Replies:
    3
    Views:
    1,286
    John H.
    Apr 27, 2010
  4. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    256
    OliverMarchand
    Apr 12, 2006
  5. cerr

    reading file round and round

    cerr, Mar 19, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    200
    Peter J. Holzer
    Mar 20, 2010
Loading...

Share This Page