How To Represent Monetary Amounts In Code // Python Tips

preview_player
Показать описание


#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

I would love a video explaining Decimals and Fractions and problems realting this topic.

CaptainCsaba
Автор

quick suggestion: clear the terminal before starting a new video. you have a few prints from another video and it's confusing when you show it at first

grmancool
Автор

someone recommended this video to me on the forum question I posted a day ago. Thanks a bunch for this!

sahil-rl
Автор

But how is my worm that siphons off all those small rounding errors that no one cares about going to work?! 🤣

Love your videos - keep ‘em up!

brettbyrnes
Автор

I would suggest using decimals instead as they preserve more details (decimal places) while doing calculations. Of course you need to care about rounding then.

Another reason would be that different currencies work in different ways - they might actually have more or less decimal places (0 to 4). Integers cannot really cover this. At least not without some additional hassle and serious headaches.

So my suggestion would be to never use integers. 😉

ddanier
Автор

Tryton ERP (ERP = Enterprise Resource Planning) is programed in Python, and uses decimal.Decimal for currency mapped to Numeric in the SQL Backend.

luisandraschnik
Автор

There is also that pypi package called "money" which tries to cover compuation and rendering issues, but it's not so easy to use and introduces overload.

davidt
Автор

it's a good technique. for live use case, stripe payment using this technique for charging the users. i will use it in live project to see how it goes. i was using float with round and it was a bit hard to handle it but working fine.

djangodeveloper
Автор

Great tip. I will use it. Please turn down music volume (unless its techno of course).

taylormonacelli
Автор

Your video on asyncio was very helpful, could you do one on Trio ? Thanks :)

clementbertaux
Автор

How do you color code the console and add the git branch?

diegolramirez
Автор

I think you will still get rounding errors even with integer division, Python masks the true float representation and shows it cleanly. For example even 0.1 is not 0.1 in float. So I think better way to handle the fractions are using decimals which guarantee exactness. I am no expert in this topic, Please correct me if I am wrong.

kireeti
Автор

Would it be fine to create a Decimal(float) class that overrides the __str__() method with round(super().__str__(), 2)?
I guess that violates lyskov sub but this way computations would be coherent (e.g. (x*.5)*2==x) which makes a huge difference when applying interest.

Also I'd say the cent solution makes sense if amounts are only used internally, but as a data scientist I get the data as dollar amounts (with a random choice of decimal and thousand seperators).

foreplate
Автор

or you can just format with f strings and also multiply by 1.18 for VAT so you dont have to add it later to the final bil

MynamedidntFitDonkey
Автор

Totally agree you shouldn't use floats. However, I can't really see a reason to use this over the decimal package, would love it if someone could provide one.

Using cents has several disadvantages imho:
1. Incurs mental cost to change the way you think about prices.
2. You have to fidget with .2f etc when displaying prices.
3. Sometimes prices need to be stored with more decimal points, e.g. for taxing purposes. In such a case you'll be back to using floats.

I will say the decimal .quantize method can be a little annoying to use, but you can write a wrapper function around it to make your life easier.

interwebslinger
Автор

Surely you can get by using the floats and the .2f formatting alone? Using the integer value divided by a 100 returns a float leading to the same situation as with the original method...

slickgamer
Автор

Definitely don’t agree for variety of topics where 2 decimals don’t cut it like stocks and foreign exchange. You should make that caveat!

sambroderick
Автор

Or, even better, just use COBOL for all calculations, with fixed-point decimal arithmetic🙂

antebilic
visit shbcf.ru