Velocity Reviews > C++ > calculate approx of decimal number ?..

# calculate approx of decimal number ?..

mark
Guest
Posts: n/a

 06-13-2004
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.

Victor Bazarov
Guest
Posts: n/a

 06-14-2004
"mark" <(E-Mail Removed)> 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.

Marcin Kalicinski
Guest
Posts: n/a

 06-14-2004
Uzytkownik "mark" <(E-Mail Removed)> napisal w wiadomosci
news:(E-Mail Removed) m...
> 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
Guest
Posts: n/a

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

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

Cheers,
Marcin