Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!lsuc!dave From: dave@lsuc.UUCP Newsgroups: comp.lang.c,comp.unix.questions Subject: printf %f rounds Message-ID: <1767@lsuc.UUCP> Date: Sun, 3-May-87 04:45:43 EDT Article-I.D.: lsuc.1767 Posted: Sun May 3 04:45:43 1987 Date-Received: Sun, 3-May-87 09:30:59 EDT Organization: Law Society of Upper Canada, Toronto Lines: 21 This is probably old news to many people, but I was startled to have it bite me. I've been programming in C for 11 years and I thought I knew how printf behaved. It appears that the behaviour of printf("%6.0f", x); while standardized, is not documented in the v7 or System V manuals. (X3J11 documents it correctly.) printf rounds to the appropriate number of digits. I've usually written things like printf("%6d", (int)(x+0.5)); in the past, rounding by hand. I happened to use %.0f (and no cast) recently, and I was surprised to find out that the +0.5 produces wrong results in this case. David Sherman The Law Society of Upper Canada Toronto -- { seismo!mnetor cbosgd!utgpu watmath decvax!utcsri ihnp4!utzoo } !lsuc!dave