Rounding

Discussion in 'C++' started by deanfamily11, Sep 14, 2005.

  1. deanfamily11

    deanfamily11 Guest

    Say I have the number 15.7, how can I round it up to 16?
    deanfamily11, Sep 14, 2005
    #1
    1. Advertising

  2. deanfamily11 wrote:
    > Say I have the number 15.7, how can I round it up to 16?


    Add 0.5 and assign it to an integer.

    V
    Victor Bazarov, Sep 14, 2005
    #2
    1. Advertising

  3. deanfamily11

    Guest

    floor(x+0.5)
    , Sep 14, 2005
    #3
  4. <> wrote in message
    news:...
    > floor(x+0.5)


    ceil(x);

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 14, 2005
    #4
  5. Ali Çehreli wrote:
    > <> wrote in message
    > news:...
    >> floor(x+0.5)

    >
    > ceil(x);


    That would be ill-advised. ceil(0.1) gives 1, which actually
    should be 0 AFA rounding is concerned, no?
    Victor Bazarov, Sep 14, 2005
    #5
  6. "Victor Bazarov" <> wrote in message
    news:...
    > Ali ehreli wrote:
    >> <> wrote in message
    >> news:...
    >>> floor(x+0.5)

    >>
    >> ceil(x);

    >
    > That would be ill-advised. ceil(0.1) gives 1, which actually
    > should be 0 AFA rounding is concerned, no?


    I realized it as soon as I posted my response; still, I will use the
    incomplete requirement as an excuse:

    "Say I have the number 15.7, how can I round it up to 16?"

    :)

    Ali

    Note: I've found at least one reference on the web, which mentions the use
    of a ceil-like functionality under the title "Rounding up." :)

    http://support.microsoft.com/default.aspx?scid=kb;en-us;196652
    =?utf-8?Q?Ali_=C3=87ehreli?=, Sep 14, 2005
    #6
  7. Ali Cehreli wrote:
    > "Victor Bazarov" <> wrote in message
    > news:...
    >> Ali ehreli wrote:
    >>> <> wrote in message
    >>> news:...
    >>>> floor(x+0.5)
    >>>
    >>> ceil(x);

    >>
    >> That would be ill-advised. ceil(0.1) gives 1, which actually
    >> should be 0 AFA rounding is concerned, no?

    >
    > I realized it as soon as I posted my response; still, I will use the
    > incomplete requirement as an excuse:
    >
    > "Say I have the number 15.7, how can I round it up to 16?"
    >
    > :)
    >
    > Ali
    >
    > Note: I've found at least one reference on the web, which mentions
    > the use of a ceil-like functionality under the title "Rounding up." :)
    >
    > http://support.microsoft.com/default.aspx?scid=kb;en-us;196652


    Yes, we both used what is allowed on Usenet and sometimes leads to
    unexpected results -- assumptions :) I assumed the requirement was
    to round in the general sense, you used the precise requirements
    stated in the posting. Either can be incorrect.

    V
    Victor Bazarov, Sep 14, 2005
    #7
  8. deanfamily11

    Pete Becker Guest

    Victor Bazarov wrote:
    >
    > That would be ill-advised. ceil(0.1) gives 1, which actually
    > should be 0 AFA rounding is concerned, no?
    >


    Usually, but if the rounding mode is round upward or round away from
    zero then 1 is right (but not right for negative numbers in the latter
    case). <g>

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Sep 14, 2005
    #8
  9. deanfamily11

    M Guest

    On Tue, 13 Sep 2005 18:05:23 -0700, Ali Çehreli <>
    wrote:

    >I realized it as soon as I posted my response; still, I will use the
    >incomplete requirement as an excuse:
    >
    >"Say I have the number 15.7, how can I round it up to 16?"
    >
    >:)
    >
    >Ali
    >


    He didn't say up or nearest so it's a pretty good excuse.

    He also didn't say what he wanted to do with the rounded value.

    Usually, when I'm rounding, it's for printing output. If that's the
    case here, then one can use formatting:

    printf("The number is: %.0f\n", num");
    or
    cout.setf(cout.fixed, cout.floatfield);
    cout.precision(0);
    cout << "The number is: " << num << endl;
    M, Sep 14, 2005
    #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. walala
    Replies:
    12
    Views:
    1,871
  2. valentin tihomirov

    rounding to integer

    valentin tihomirov, Feb 15, 2004, in forum: VHDL
    Replies:
    2
    Views:
    9,580
    Jonathan Bromley
    Feb 16, 2004
  3. C

    Rounding Numbers

    C, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    489
  4. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    499
    Karl Seguin
    Nov 22, 2005
  5. iksrazal
    Replies:
    1
    Views:
    727
    Chris Smith
    Jul 3, 2003
Loading...

Share This Page