-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Friendly Formatting #88
Comments
I just came across this library, and even though this question is old I'll answer this in case someone else stumbles across it. Caveat: Below I give 2 ways to do this using only Python's stdlib. However the Formatting the Money using regular money formatPython's string formatting will handle number grouping: value = Money("1234567.86", Currency.USD)
f"${value.amount:,}" Output: '$1,234,567.86' If you want to use a locale-aware separator just specify import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
value = Money("1234567.86", Currency.EUR)
f"{value.amount:n} €" Output: '1.234.567,86 €' It's easier to read if you use the locale.currency(value.amount, symbol=True, grouping=True) Output: '1.234.567,86 €' Truncate to an integer if modulo 1 == 0I struggle to see the use case for this question. Keep in mind that if you use the def maybe_truncate(value: Money) -> int | Decimal:
if value % 1 == 0:
return int(value)
return value.as_decimal()
value = Money("1234567.86", Currency.USD)
f"${maybe_truncate(value):n}"
-> $1,234,567.86
value += 0.14
f"${maybe_truncate(value):n}"
-> $1,234,568 |
from babel.numbers import format_currency that way you can easily formate currency in INR |
Firstly, this is a fantastic library.
I have a few questions relating to readability of the string output:
1000000.00 USD
as1,000,000.00 USD
? (humanize.intcomma
module does this)$1000.00
instead of1000.00 USD
?1000.00 USD -> 1000 USD
?The text was updated successfully, but these errors were encountered: