# Rounding

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

1. ### deanfamily11Guest

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

deanfamily11, Sep 14, 2005

2. ### Victor BazarovGuest

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

3. ### Guest

floor(x+0.5)

, Sep 14, 2005
4. ### =?iso-8859-1?Q?Ali_=C7ehreli?=Guest

<> wrote in message
news:...
> floor(x+0.5)

ceil(x);

Ali

=?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 14, 2005
5. ### Victor BazarovGuest

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
6. ### =?utf-8?Q?Ali_=C3=87ehreli?=Guest

"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
7. ### Victor BazarovGuest

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
8. ### Pete BeckerGuest

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
9. ### MGuest

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