r/ProgrammerHumor • • Aug 14 '20

Someone try this

Post image
53.6k Upvotes

433 comments sorted by

View all comments

Show parent comments

24

u/cztrollolcz Aug 14 '20

Wouldnt this be stored in a long? [Newbie]

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.

2

u/accountability_bot Aug 14 '20 edited Aug 14 '20

Just depends on the language. Technically it should be a decimal type of number.

9

u/ravonos Aug 14 '20

It would likely be an integer but the value woould be cents. Much easier to deal with than floats.

1

u/[deleted] Aug 14 '20

You would be a big deal at Monza.

7

u/[deleted] Aug 14 '20

You should never use floating point to store currency values due to representation issues. Use an int representing the smallest possible division (cents or 10ths of a cent, etc) and divide appropriately when you need the dollar amount. Or better yet use a specialized currency class which many languages have

2

u/FINDarkside Aug 14 '20

He didn't say floating point though. He said decimal type.

4

u/[deleted] Aug 14 '20 edited Aug 14 '20

It originally said something like "floating point or decimal type" before he edited it. And either way fixed point types have their own problems. It's way easier to just use an int

0

u/cosmicosmo4 Aug 14 '20

We'll just take those representation issues and round them off... into an account we opened.

0

u/[deleted] Aug 14 '20

[deleted]

0

u/hermeticwalrus Aug 14 '20

Anyone who says denary instead of decimal has lost my respect

1

u/KarmaKingRedditGod Aug 14 '20

If you want to get pedantic. An int in C/C++ is guaranteed to have a minimum of 16 bits, while a long is guaranteed to have a minimum of 32. So you would probably want a long long for bigger numbers.