Re: Should %d round?
- Posted by mattlewis (admin) Mar 20, 2012
- 1727 views
DerekParnell said...
Also consider the following ...
atom x = 3.552 printf(1, "%d\n", x) -->3 printf(1, "%.0f\n", x) -->4 printf(1, "%.1f\n", x) -->3.6 printf(1, "%.2f\n", x) -->3.55 printf(1, "%.3f\n", x) -->3.552
The documentation needs to make clear that %d will only print the integer portion of a number. If one wants the decimals to influence the output you need to use the %f code instead, specifying the required rounding location.
Yes, I think those examples would be very good. I was considering adding language to the effect that using %d is equivalent to passing your value through floor(). At that point, I think the documentation will be pretty solid.
Matt