Re: Should %d round?

new topic     » goto parent     » topic index » view thread      » older message » newer message
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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu