home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.text
- Path: sparky!uunet!gatech!usenet.ins.cwru.edu!eagle!ariel.lerc.nasa.gov!smneyln
- From: smneyln@ariel.lerc.nasa.gov (Michael Neylon)
- Subject: Re: Rounding function in c
- Message-ID: <29JUL199207101332@ariel.lerc.nasa.gov>
- Followup-To: comp.text
- News-Software: VAX/VMS VNEWS 1.41
- Sender: news@eagle.lerc.nasa.gov
- Organization: NASA Lewis Research Center
- References: <1992Jul14.181421.24591@mr.med.ge.com> <1992Jul16.232926.4740@rossinc.com> <451@rascal.UUCP>
- Date: 29 Jul 1992 07:10 EST
- Lines: 39
-
- In article <451@rascal.UUCP>, theo@rascal.UUCP (T. Kramer 794-1472) writes...
- >In article <1992Jul16.232926.4740@rossinc.com> ciaranst@rossinc.com (ciaran storan) writes:
- >>
- >>Hi,
- >> Does anyone out there have a rounding function for ANSI 'C', I am
- >>trying to do some financial applications, and badly need a rounding
- >>function.
- >>
- >>Any HELP??.
- >
- >Me too please.
- >
- >please email me theo@rascal.UUCP
-
- /* rounds num to nearest base */
- int round(float num, int base)
- {
- int retval;
- num += base/2;
- retval = ((int) num)/base;
- retrun retval*base;
- }
-
- so if i had 5632 and I wanted to round it to the hundreds place...
- 5632 +(100)/2 = 5682;
- 5682/100 = 56.82 = 56 (int math drops the fractional part)
- 56 *100 = 5600;
-
- and for 5683
- 5683 + 50 = 5733
- 5733/100 = 57
- 57*100 = 5700
-
- Mike Neylon aka Masem the Great and Almighty Thermo GOD
- // Only the | Univ. of Toledo - Chemical Engineering - Senior
- \X/ AMIGA! | NASA Lewis Research Center - Summer Intern
- \_____________|_____smneyln@ariel.lerc.nasa.gov________________/
- -+ How do YOU spell 'potato'? How about 'lousy'? +-
- "Me and Spike are big Malcolm 10 supporters."-J.S.,PLCL
-