2014-01-29 59 views
0

我的货币固定在可能不是最好的lookign方式,但它的工作原理,我现在需要的是货币符号将放在字符串的末尾。 当我尝试p_sign_posn = 3在不同的语言环境功能,我不能得到它的工作。Python语言环境在字符串末尾设置货币

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 ') 

回答

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

我不知道的一种方式在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