babel icon indicating copy to clipboard operation
babel copied to clipboard

Currency normalization

Open szewczykmira opened this issue 8 years ago • 6 comments

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?

szewczykmira avatar Mar 10 '17 10:03 szewczykmira

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 €

akx avatar Mar 30 '17 10:03 akx

@szewczykmira : That's more or less what I did with my unround_pattern() method from https://github.com/python-babel/babel/issues/90#issuecomment-174456874. I hack the default format pattern of a local to not let it truncate amounts with lots of decimals. I guess you can reuse it and tweak it to your own needs.

kdeldycke avatar Apr 07 '17 07:04 kdeldycke

This is addressed by #494.

kdeldycke avatar Oct 18 '17 13:10 kdeldycke

@akx can this zero-stripping feature be added as an option to format_currency()

igo avatar Jun 07 '20 18:06 igo

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.

picturedots avatar Sep 22 '21 20:09 picturedots

Hello, we're also interested in this feature! The workaround using the NumberPattern works for us, but we noticed that the force_frac parameter is deprecated https://github.com/python-babel/babel/blob/d7a7589a6cee3aa4c68de60f4d69a9cdad50a7ff/babel/numbers.py#L1409-L1410

We might be able to craft a PR supporting this usecase but we're not familiar with the library internals (or if there are plans regarding the whole module). What would be the main steps in order to implement this properly? (ie: what would be a decent DX for the parameters? what internal classes should me modify?)

Thank you in advance!

QuentinFchx avatar Apr 11 '25 08:04 QuentinFchx