Here's a fun little problem. Suppose you've got a system that makes use of the following struct:
struct User {
unsigned int user_id;
unsigned short access_level;
float account_balance; // stored as US dollars
}Your system is ridiculously well-tested, all the tests pass, everything works totally fine. I'll be so bold as to say it's (kinda)
(
Read more... )
If you need to work with money, store it as an integer to ensure that addition, multiplication, and comparisons work correctly. If you need to deal with non-integer amounts of money, just make your units sufficiently small (for instance, you could store everything in microcents). The important part is to ensure that you will neither create nor destroy money as you process it.
Reply
Reply
Reply
Reply
Leave a comment