home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16239 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  918 b 

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rutgers!psuvax1!psuvm!ia8
  2. From: IA8@psuvm.psu.edu (Alan R. Heyd)
  3. Newsgroups: comp.lang.c
  4. Subject: Rounding floats to integers
  5. Message-ID: <92314.134711IA8@psuvm.psu.edu>
  6. Date: 9 Nov 92 18:47:11 GMT
  7. Organization: Penn State University
  8. Lines: 18
  9.  
  10. Is there a C routine that will round off floating point numbers
  11. to the nearest integer.  K&R says that floats are converted to
  12. integers by removing the fractional part, however, I could not
  13. find mention of a routine which rounds to the nearest integer.
  14. What I do now is add 0.5 to the float before it is converted to
  15. an integer, for example:
  16.  
  17.           int i;
  18.                                  /* Using Turbo C on and IBM PC: */
  19.           i = 6.0 / 0.3;         /*   i = 19                     */
  20.           i = 6.0 / 0.3 + 0.5;   /*   i = 20 as expected         */
  21.  
  22. is there a better way?
  23.  
  24. Thanks,
  25.  
  26. Alan Heyd
  27. ia8@psuvm.psu.edu
  28.