Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!kth!draken!d88-jwa From: d88-jwa@nada.kth.se (Jon W{tte) Newsgroups: comp.lang.c Subject: Re: Help needed to round-off a number Message-ID: <1164@draken.nada.kth.se> Date: 4 Jun 89 14:59:00 GMT References: <2666@oregon.uoregon.edu> <267@ibd.BRL.MIL> Reply-To: d88-jwa@nada.kth.se (Jon W{tte) Organization: Royal Institute of Technology, Stockholm, Sweden Lines: 23 In article <267@ibd.BRL.MIL> heilpern@brl.arpa (Mark A. Heilpern (IBD) ) 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 --