I got my currency fixed in maybe not the best lookign way but it works, what I need now is that the currency symbol will be placed at the end of the string. When i try p_sign_posn = 3 at different functions of locale, i cant get it to work.

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 ')
有帮助吗?

解决方案

I don't know how to set values (in your case LC_MONETARY.p_sign_posn) into locale but you always can write a little function:

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)

其他提示

I'm not aware of a way to set-up your own locale in Python. One way to achieve the results you want would be like this:

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

A more involved, but more general, approach would be to use this modified version of the recipe given in the documentation for formatting currency to which I've added support for four additional keyword arguments corresponding to their names in thelocalemodule.

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top