[comp.lang.c] Help needed to round-off a number

michelbi@oregon.uoregon.edu (Michel Biedermann) (05/20/89)

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.
 
Thanks a lot for the help...
 
Michel Biedermann
U. of Oregon

chris@mimsy.UUCP (Chris Torek) (05/20/89)

In article <2666@oregon.uoregon.edu> michelbi@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.

In the proposed ANSI C standard, printf is defined as rounding when
given a floating point format with a particular precision.  Both of
these results will be obtained with a `%.1f' printf format (assuming
a Unix or pANS-conformant printf).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

tps@chem.ucsd.edu (Tom Stockfisch) (05/21/89)

In article <2666@oregon.uoregon.edu> michelbi@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@chem.ucsd.edu

heilpern@ibd.BRL.MIL (Mark A. Heilpern ) (06/03/89)

In article <2666@oregon.uoregon.edu> michelbi@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.
> 
>Thanks a lot for the help...
> 

There is a relatively painless way to do this:
1) you want to round off to 'n' places:
2) multiply your original by 10^n
2a) add (5 / 10^n) for round-up error
3) truncate the fraction off of your answer
4) divide this result by 10^n
5) you've just rounded to n decimal places.

	--M.

d88-jwa@nada.kth.se (Jon W{tte) (06/04/89)

In article <267@ibd.BRL.MIL> heilpern@brl.arpa (Mark A. Heilpern (IBD) <heilpern>) writes:
>There is a relatively painless way to do this:
>1) you want to round off to 'n' places:
>2) multiply your original by 10^n
>2a) add (5 / 10^n) for round-up error
>3) truncate the fraction off of your answer
>4) divide this result by 10^n
>5) you've just rounded to n decimal places.

Actually, you have NOT rounded correctly. For the given algorithm to
work, you need to replace line 2a) with:
2a) add 0.5 for round-up error (Regardless of what n is)

Also, this routine will NOT round statistically correctly regarding
odd/even numbers and negative numbers, but you can't have it all...
unless you REALLY want to slow things down!



-- 
 __       Jon W{tte (The dread Smiley Shark) email:h+@nada.kth.se
/  \      (+46 (0) 8 258 268)
   /---   (c) 1989 Yessbox Allright Professional Products Inc. - Y.A.P.P.I.
  /       -- No More --

scs@adam.pika.mit.edu (Steve Summit) (06/05/89)

In article <267@ibd.BRL.MIL> heilpern@brl.arpa (Mark A. Heilpern (IBD) <heilpern>) writes:
>There is a relatively painless way to [truncate or round-off
>a number a certain decimal place]:
>2) multiply your original by 10^n
>2a) add (5 / 10^n) for round-up error
>3) truncate the fraction off of your answer
>4) divide this result by 10^n

If you have normalized the intermediate result by 10^n (in step 2)
then you round by adding one half, not 5/10^n.

Note that the method lets you round to a multiple of anything,
not necessarily limited to powers of ten.  (You may want to
subtract a half and/or use ceil() when rounding negative numbers,
though.)

                                            Steve Summit
                                            scs@adam.pika.mit.edu