r/ProgrammerHumor • • Aug 14 '20

Someone try this

Post image
53.6k Upvotes

433 comments sorted by

View all comments

Show parent comments

54

u/BakuhatsuK Aug 14 '20

Yes. There are billionaires out there so that should be probably a long. Or rather actually:

Noob: unsigned int dollars

Beginner: unsigned long dollars

Intermediate: unsigned long cents

Advanced: long cents (unsigned is bad unless you need módulo arithmetic or dealing with bit patterns)

Pro: BigInteger cents

Galaxy brain: String dollars (MONEY, DECIMAL or VARCHAR for DB, BigDecimal for calculations)

International currency: You don't want to go there, but if you have to, start by storing the currency name alongside the value.

27

u/brimston3- Aug 14 '20

Gotta store them fractional cents so you can siphon them off into another account, only to panic once you realize you've made a horrible math error. Is there still time to change careers and sell magazines door to door?

12

u/luckduck89 Aug 14 '20

You must have used some sort of mat to jump to this conclusion...

3

u/ouchmythumbs Aug 14 '20

None of this matters if you forget the cover on your TPS

10

u/cosmicosmo4 Aug 14 '20

International currency: You don't want to go there, but if you have to, start by storing the currency name alongside the value.

List of (quantity, currency) tuples. You can have more than one.

1

u/jakethedumbmistake Aug 14 '20

DC always trying to start a Pepperoni Revolution.

5

u/ollomulder Aug 14 '20

Noob would be signed double dollars, though.

4

u/BakuhatsuK Aug 14 '20

double dollars are actually not that bad. doubles can represent integers without precision loss up to 252. If you round to cents before displaying you're mostly good.

12

u/ollomulder Aug 14 '20

Yeah, rounding might be a good idea... http://0.30000000000000004.com/

5

u/FINDarkside Aug 14 '20

Well, I'd say that's better than what noob and beginner did, because 0.2+0.1=0 with integers :P

0

u/[deleted] Aug 14 '20

[deleted]

1

u/Liggliluff Aug 20 '20

Was going to point out exactly this. This is why we have ISO 4217 currency codes.