forked from py-moneyed/py-moneyed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX py-moneyed#22: Deprecate current format and depend on babel
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters