Article View: comp.text
Article #162Re: Rounding function in c
From: Morten Sabroe Mortensen
Date: Sat, 01 Aug 1992 16:18
Date: Sat, 01 Aug 1992 16:18
44 lines
1598 bytes
1598 bytes
theo@rascal.UUCP (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
Well, how about this:
/* BEGIN */
#include <stdio.h>
#define Round(i,f) i=(int)f;\
if ((double)i+0.5<=f) i++
main()
{ int i;
double f;
f=123.456;
printf("Floating value: %f\n",f);
Round(i,f);
printf("Floating value rounded to an integer: %d\n",i);
f=234.567;
printf("Floating value: %f\n",f);
Round(i,f);
printf("Floating value rounded to an integer: %d\n",i);
};
/* END */
I belive that's what you are looking for!?
I myself have just invented this to be used with a graphics-program of mine!
It's on purpose I made it look like a function! Because performance is
crucial, it has been put into a #define, but of course - it could be a
function if you'd like!
Technical Details
Message-ID:
<1992Aug1.161824.5285@daimi.aau.dk>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!daimi!sabroeReferences:
<1992Jul14.181421.24591@mr.med.ge.com> <1992Jul16.232926.4740@rossinc.com> <451@rascal.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!daimi!sabroe From: sabroe@daimi.aau.dk (Morten Sabroe Mortensen) Newsgroups: comp.text Subject: Re: Rounding function in c Message-ID: <1992Aug1.161824.5285@daimi.aau.dk> Date: 1 Aug 92 16:18:24 GMT References: <1992Jul14.181421.24591@mr.med.ge.com> <1992Jul16.232926.4740@rossinc.com> <451@rascal.UUCP> Sender: sabroe@daimi.aau.dk (Morten Sabroe Mortensen) Organization: DAIMI: Computer Science Department, Aarhus University, Denmark Lines: 44