我的货币固定可能不是最好的外观方式,但它有效,我现在需要的是货币符号将放在字符串的末尾。 当我在语言环境的不同功能上尝试p_sign_posn = 3时,我无法让它工作。
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
bruto = locale.currency(total, grouping=True).replace('$', 'ISK ')
tax = locale.currency(tax, grouping=True).replace('$', 'ISK ')
netto = locale.currency(netto, grouping=True).replace('$', 'ISK ')
答案 0 :(得分:2)
我不知道如何将值(在您的情况下为LC_MONETARY.p_sign_posn)设置为语言环境,但您总是可以编写一个小函数:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
def addAndReverseSign(number, grouping=True):
sign, number = locale.currency(number, grouping).replace('$', 'ISK ').split(' ')
print number, sign
addAndReverseSign(123)
答案 1 :(得分:1)
我不知道在Python中设置自己的语言环境的方法。实现所需结果的一种方法是:
import locale
locale.setlocale(locale.LC_ALL, 'icelandic')
total, tax, netto = 100.00, 8.32, 108.32
bruto = locale.currency(total, grouping=True).replace('kr.', 'ISK ')
tax = locale.currency(tax, grouping=True).replace('kr.', 'ISK ')
netto = locale.currency(netto, grouping=True).replace('kr.', 'ISK ')
print bruto # 100,00 ISK
print tax # 8,32 ISK
print netto # 108,32 ISK
更复杂但更通用的方法是使用recipe given in the documentation for formatting currency的这个修改版本,我已经添加了对locale
模块中与其名称相对应的四个附加关键字参数的支持
from decimal import Decimal
def moneyfmt(value, places=2, curr='', sep=',', dp='.',
pos='', neg='-', trailneg='',
p_cs_precedes=True, n_cs_precedes=True,
p_sep_by_space=False, n_sep_by_space=False):
"""Convert numeric value to a money formatted string.
places: required number of places after the decimal point
curr: optional currency symbol before the sign (may be blank)
sep: optional grouping separator (comma, period, space, or blank)
dp: decimal point indicator (comma or period)
only specify as blank when places is zero
pos: optional sign for positive numbers: '+', space or blank
neg: optional sign for negative numbers: '-', '(', space or blank
trailneg: optional trailing minus indicator: '-', ')', space or blank
p_cs_precedes: currency symbol precedes positive value*
n_cs_precedes: currency symbol precedes negative value*
p_sep_by_space: currency symbol separated by space from the positive value*
n_sep_by_space: currency symbol separated by space from the negative value*
(keywords added*)
>>> d = Decimal('-1234567.8901')
>>> moneyfmt(d, curr='$')
'-$1,234,567.89'
>>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
'1.234.568-'
>>> moneyfmt(d, curr='$', neg='(', trailneg=')')
'($1,234,567.89)'
>>> moneyfmt(Decimal(123456789), sep=' ')
'123 456 789.00'
>>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
'<0.02>'
"""
if not isinstance(value, Decimal):
value = Decimal(value)
q = Decimal(10) ** -places # 2 places --> '0.01'
sign, digits, exp = value.quantize(q).as_tuple()
result = []
digits = map(str, digits)
build, next = result.append, digits.pop
if p_cs_precedes:
build(curr)
if p_sep_by_space:
build(' ')
if sign:
build(trailneg)
for i in range(places):
build(next() if digits else '0')
build(dp)
if not digits:
build('0')
i = 0
while digits:
build(next())
i += 1
if i == 3 and digits:
i = 0
build(sep)
build(neg if sign else pos)
if not p_cs_precedes:
if p_sep_by_space:
build(' ')
build(curr)
return ''.join(reversed(result))
# try all the combinations of new options on a positive and negative number
from itertools import izip, product
kwrds = 'p_cs_precedes', 'n_cs_precedes', 'p_sep_by_space', 'n_sep_by_space'
for netto in [100, -100]:
for combo in product((False,True), (False,True), (False,True), (False,True)):
kwargs = dict(izip(kwrds, combo))
print '{!r}'.format(moneyfmt(netto, curr='ISK', **kwargs))
print