|Keep It Simple, Stupid|
Re^2: Printf/Sprintf Behavior Change (stupid printf)by tye (Sage)
|on Sep 16, 2013 at 19:20 UTC||Need Help??|
The problem is NOT that printf is stupid.
Yes, if you implement your own "round to 2 decimal places" algorithm that doesn't actually round but instead truncates, then you'll (often) get a different answer than if you actually round (whether or not you use sprintf to actually round, which is one of the better choices available).
Specifically round arithmetically using your own rules and you should at least get consistent results.
See, that just proves that you didn't learn the lesson that a bunch of the other replies in this thread were trying to teach.
It is just that your arithmetic adds more floating point operations that can change the inconsistency! I can find cases where your particular arithmetic happens to be more consistent than sprintf's. Above I show a case where sprintf() is more consistent.
Now go read the other replies and/or what they link to more carefully and learn why.