PDA

Click to See Complete Forum and Search --> : Rounding......


Smie
Apr 26th, 2002, 09:28 AM
What header file and function will let me round in C++, thanks a bunch!

CornedBee
Apr 26th, 2002, 09:50 AM
None, here is keda's round function:
inline int round(const double d)
{ return (int)(d + 0.5); }

jim mcnamara
Apr 26th, 2002, 10:30 AM
This rounds as well


sprintf(tmp,"%5.2f",mydouble);
mydouble=ecvt(tmp);


the .2 part sets the number of digits after the decimal - and it actually rounds up.

jim mcnamara
Apr 26th, 2002, 10:40 AM
a more general rounding function


#include <stdio.h>
double round(double p, int placesbefore,int placesafter){
char tmp[20];
sprintf(tmp,"%*.*f",placesbefore,placesafter,p);
return ecvt(tmp);
}


usage: mydouble = round(mydouble,5,2);