Skip to content

Commit

Permalink
FIX py-moneyed#22: Deprecate current format and depend on babel
Browse files Browse the repository at this point in the history
  • Loading branch information
pooyamb committed May 30, 2019
1 parent 1822e9f commit 718b201
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions moneyed/l10n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import re

from babel.numbers import format_currency as babel_format_currency, LC_NUMERIC
from babel.core import Locale


def format_currency(money, *args, **kwargs):
return babel_format_currency(money.amount, money.currency.code, *args, **kwargs)


DECIMAL_PLACES_REGEX = re.compile('\.0*')


def format_money(money, include_symbol=True, locale=LC_NUMERIC,
decimal_places=None, rounding_method=None):
locale = Locale.parse(locale)
format = locale.currency_formats['standard'].pattern
if not include_symbol:
format = format.replace('¤', '').strip()
if decimal_places:
zeroes = '0' * decimal_places
format = re.sub(DECIMAL_PLACES_REGEX, '.' + zeroes, format)

return format_currency(money, format=format, locale=locale, currency_digits=False, decimal_quantization=True)
2 changes: 2 additions & 0 deletions moneyed/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import unicode_literals
from decimal import Decimal, ROUND_HALF_EVEN
import moneyed
import warnings

DEFAULT = "DEFAULT"

Expand Down Expand Up @@ -56,6 +57,7 @@ def get_formatting_definition(self, locale):

def format(self, money, include_symbol=True, locale=DEFAULT,
decimal_places=None, rounding_method=None):
warnings.warn('This method is deprecated in favour of new i18n module.', Warning)
locale = locale.upper()
code = money.currency.code.upper()
prefix, suffix = self.get_sign_definition(code, locale)
Expand Down

0 comments on commit 718b201

Please sign in to comment.