Help needed to round-off a number

Tom Stockfisch tps at chem.ucsd.edu
Sun May 21 11:40:57 AEST 1989


In article <2666 at oregon.uoregon.edu> michelbi at oregon.uoregon.edu (Michel Biedermann) writes:
>Is there a C function (user defined or primitive (?)) that will let me
>truncate or round-off a number UP TO a certain decimal place.  For example
>transforming .567 into .6 or -1.502 into -1.5.


If speed isn't crucial, use

double
round( x, sigfigs )
	double	x;
	int	sigfigs;
{
	char	buf[sizeof(double)*20];	/* nice conservative size */

	sprintf( buf, "%.*g", x, sigfigs );
	sscanf( buf, "%lf", &x );
	return	x;
}
-- 

|| Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu



More information about the Comp.lang.c mailing list