[comp.lang.c] printf %f rounds

dave@lsuc.UUCP (05/03/87)

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