Article View: comp.text
Article #46Re: Rounding function in c
From: Stephen Riehm
Date: Thu, 30 Jul 1992 07:29
Date: Thu, 30 Jul 1992 07:29
124 lines
3788 bytes
3788 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.
try the following:
---- included copy of message sent to original poster of this problem ----
this may not be exactly what you are looking for, but using casts you
can don something like what I have included below. I am not in any way
an ANSI 'C' programmer, K&R is more my style, but this might at least
give you some ideas....
I tested it quickly and it works quite well. ( but I didn't bother
doing efficiency tests though, so someone else might have a quicker
hack )
( in fact I just looked and we don't even have an ANSI compiler on
this machine.. oh well..the theory is the same! )
good luck!
-----------------------------------------------------------------
Stephen Riehm smr@pki-nbg.philips.de
not in any way talking on behalf of:
Philips Kommunikations Industrie Phone: +49 911 526 2975
8500 Nuremberg, Germany Fax: +49 911 526 2095
I come from the land down under, where women glow and men chunder
--- cut here --- file: /home/smr/bin/.s/round.c
/*-----------------------------------------------------------------------------
File: round.c
Written by: Stephen Riehm
Date: 20/07/92 14:50
Description:
round numbers
*/
/*
* ----- includes -----
*/
#include <stdio.h>
/*
* externs
*/
float round();
/*
* ----- functions -----
*/
main( )
{
float orig = 12.65432; /* original number to round */
float result; /* orig rounded to 1 dec. place */
int i; /* loop counter */
for( i = 0; i < 5; i++ )
{
result = round( orig, i ); /* round to i decimal places */
printf( "%f rounded to %d places = %.*f\n", orig, i, i, result );
}
}
/*-----------------------------------------------------------------------------
round: basic idea:
multiply the number by 10^( however many decimal
places you want +1 )
cast the resulting number into an int ( this strips the
bits behind the . )
add 5 ( because we shifted one dec. pos. too far this will
only effect the outcome if the last digit is 5 or
greater )
divide the integer by 10. ( this loses the 5 from the end )
convert the number back to a float and divide by 10 ^
( the number of decimal places ) to get the true result.
OK its not straight forward, but it works! and its the most
reasonable way I know of actually rounding ( and not just
truncating )
*/
float round( orig, exp )
float orig;
int exp;
{
int tmp_int; /* temporary integer */
float rtn; /* return value */
tmp_int = orig * ( float )power( 10, exp + 1 );
/* ^-- trunctation takes place here! */
tmp_int += 5; /* now round to the NEAREST number */
tmp_int /= 10; /* strip the 5 off */
/* move the decimal point back where it belongs! */
rtn = ( float )tmp_int / ( float )power( 10, exp );
return( rtn );
}
power( base, exp )
int base;
int exp;
{
int rtn = 1; /* return value */
while( exp-- > 0 )
{
rtn = rtn * base;
}
return( rtn );
}
--- cut here ---
Technical Details
Message-ID:
<smr.712481385@hitkw14>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!unido!pki-nbg!hitkw14!smrReferences:
<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!Germany.EU.net!unido!pki-nbg!hitkw14!smr From: smr@iti.org (Stephen Riehm) Newsgroups: comp.text Subject: Re: Rounding function in c Message-ID: <smr.712481385@hitkw14> Date: 30 Jul 92 07:29:45 GMT References: <1992Jul14.181421.24591@mr.med.ge.com> <1992Jul16.232926.4740@rossinc.com> <451@rascal.UUCP> Sender: news@pki-nbg.philips.de Lines: 124