# calculate approx of decimal number ?..

Discussion in 'C++' started by mark, Jun 14, 2004.

1. ### markGuest

How to calculate the approximation of a given decimal number, say for
two places after decimal.

like 10.987 should be approximated to 10.99
10.947 should be approximated to 10.95

can someone point me to a web site where one can find implementations
of standard math library functions like say floor, ceil etc.

mark, Jun 14, 2004

2. ### Victor BazarovGuest

"mark" <> wrote...
> How to calculate the approximation of a given decimal number, say for
> two places after decimal.
>
> like 10.987 should be approximated to 10.99
> 10.947 should be approximated to 10.95

It's called "rounding" and you can find different methods of
achieving that in different books. Or on Google.

> can someone point me to a web site where one can find implementations
> of standard math library functions like say floor, ceil etc.

Someone possibly can. I can't. Oh, wait, www.google.com
should be of some help.

Victor Bazarov, Jun 14, 2004

3. ### Marcin KalicinskiGuest

Uzytkownik "mark" <> napisal w wiadomosci
news:...
> How to calculate the approximation of a given decimal number, say for
> two places after decimal.
>
> like 10.987 should be approximated to 10.99
> 10.947 should be approximated to 10.95

It's really simple:

double RoundTo2PlacesAfterDecimal(double x)
{
return floorf(x * 100 + 0.5) / 100;
}

Best regards,
Marcin

Marcin Kalicinski, Jun 14, 2004
4. ### Marcin KalicinskiGuest

> double RoundTo2PlacesAfterDecimal(double x)
> {
> return floorf(x * 100 + 0.5) / 100;
> }

Should be "floor" of course, not "floorf".

Cheers,
Marcin

Marcin Kalicinski, Jun 14, 2004