question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Currency normalization

See original GitHub issue

In my project I need to format currency so that it won’t display digital decimals if they are equal to zero. Right now I’m doing it by replacing .00 to .## in current locale pattern and passing it as format to format_currency and setting currency_digits=False. It works like charm when price has 0 or 2 decimal digits, but I’m not sure how to handle prices with 3 decimal digits. Can I safely replace .00 with . + '#' * decimal_digits or it will cause problems I’m not aware of? Or maybe there is better solution for that?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
akxcommented, Mar 30, 2017

Am I understanding this correctly:

  • If the value is integer, print no decimals
  • If the value is not integer, print as many decimals as the currency usually would

If so, you can adapt the innards of format_currency (though I admit being able to wholesale override the number of decimals would be handy):

from babel import Locale
from babel.numbers import decimal

def custom_format_currency(value, currency, locale):
    value = decimal.Decimal(value)
    locale = Locale.parse(locale)
    pattern = locale.currency_formats['standard']
    force_frac = ((0, 0) if value == int(value) else None)
    return pattern.apply(value, locale, currency=currency, force_frac=force_frac)

print(custom_format_currency('2.50', 'EUR', 'fi'))
print(custom_format_currency('2.00', 'EUR', 'fi'))

outputs

2,50 €
2 €
0reactions
picturedotscommented, Sep 22, 2021

Bumping this – I have a use case where I sometimes want to display currency without any decimals, but not have a solution that requires a different format string for each locale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Types of Data Normalization
Mercer WIN provides for two types of data normalization: Scaling and Currency Conversion. Scaling. Allows you to define the format in which market...
Read more >
Currency Normalizer - PostHog
Currency Normalizer. Simplify your data by normalizing multiple currencies in PostHog events into a single currency of your choice.
Read more >
What Is Monetary Policy Normalization? | St. Louis Fed
Transform the Fed's asset holdings to a composition similar to those of pre-Great Recession times. This transformation will involve a reduction in the...
Read more >
Policy Normalization - Federal Reserve Board
The Committee views changes in the target range for the federal funds rate as its primary means of adjusting the stance of monetary...
Read more >
Normalizing Monetary Policy | Cato Institute
The current focus of Federal Reserve policy is on “normalization” of monetary policy—that is, on increasing short‐​term interest rates and shrinking the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found