Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!haven!adm!smoke!ibd!heilpern From: heilpern@ibd.BRL.MIL (Mark A. Heilpern ) Newsgroups: comp.lang.c Subject: Re: Help needed to round-off a number Message-ID: <267@ibd.BRL.MIL> Date: 2 Jun 89 17:47:07 GMT References: <2666@oregon.uoregon.edu> Reply-To: heilpern@brl.arpa (Mark A. Heilpern (IBD) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 17 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.