Help needed to round-off a number

Mark A. Heilpern heilpern at ibd.BRL.MIL
Sat Jun 3 03:47:07 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.
> 
>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.



More information about the Comp.lang.c mailing list