Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: Help needed to round-off a number Message-ID: <11832@bloom-beacon.MIT.EDU> Date: 4 Jun 89 18:39:04 GMT References: <2666@oregon.uoregon.edu> <267@ibd.BRL.MIL> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 18 In article <267@ibd.BRL.MIL> heilpern@brl.arpa (Mark A. Heilpern (IBD) ) 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