home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rutgers!psuvax1!psuvm!ia8
- From: IA8@psuvm.psu.edu (Alan R. Heyd)
- Newsgroups: comp.lang.c
- Subject: Rounding floats to integers
- Message-ID: <92314.134711IA8@psuvm.psu.edu>
- Date: 9 Nov 92 18:47:11 GMT
- Organization: Penn State University
- Lines: 18
-
- Is there a C routine that will round off floating point numbers
- to the nearest integer. K&R says that floats are converted to
- integers by removing the fractional part, however, I could not
- find mention of a routine which rounds to the nearest integer.
- What I do now is add 0.5 to the float before it is converted to
- an integer, for example:
-
- int i;
- /* Using Turbo C on and IBM PC: */
- i = 6.0 / 0.3; /* i = 19 */
- i = 6.0 / 0.3 + 0.5; /* i = 20 as expected */
-
- is there a better way?
-
- Thanks,
-
- Alan Heyd
- ia8@psuvm.psu.edu
-