Nik*_*ntz 2 python django currency
我得到不同货币的价格,并希望显示巴西R $我的格式不起作用,显示如下:
价格:1.15..000.,00 R $
为了获得良好的灵活性,我将价格存储为字符串: price=db.StringProperty(verbose_name="price")
我试图实现我自己的过滤器,它不起作用:
{{ ad.price|separate }} R$
def separate(n, sep='.'):
ln = list(str(n))
ln.reverse()
newn = []
while len(ln) > 3:
newn.extend(ln[:3])
newn.append(sep)
ln = ln[3:]
newn.extend(ln)
newn.reverse()
return "".join(newn)
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?我应该删除过滤器吗?我应该对输入执行一些正则表达式吗?我网站的链接是http://www.koolbusiness.com/servead/4252196
更新:我正在考虑使用像这些过滤器之一:
import locale
locale.setlocale(locale.LC_ALL, '')
def currency(value): # doesn't work
locale.setlocale(locale.LC_ALL, '')
return locale.currency(value, grouping=True)
register.filter(currency)
def currencyWithoutUsingLocale(value): # needs adjustment
value=float(value)
symbol = '$'
thousand_sep = ''
decimal_sep = ''
# try to use settings if set
try:
symbol = settings.CURRENCY_SYMBOL
except AttributeError:
pass
try:
thousand_sep = settings.THOUSAND_SEPARATOR
decimal_sep = settings.DECIMAL_SEPARATOR
except AttributeError:
thousand_sep = ','
decimal_sep = '.'
intstr = str(int(value))
f = lambda x, n, acc=[]: f(x[:-n], n, [(x[-n:])]+acc) if x else acc
intpart = thousand_sep.join(f(intstr, 3))
return "%s%s%s%s" % (symbol, intpart, decimal_sep, ("%0.2f" % value)[-2:])
register.filter(currencyWithoutUsingLocale)
Run Code Online (Sandbox Code Playgroud)
将价格存储为字符串是第一个问题.它应该是一个十进制.如果你查看Decimal的Python标准库文档,你会看到这个http://docs.python.org/library/decimal.html#recipes
moneyfmt食谱应该做你想要的
| 归档时间: |
|
| 查看次数: |
790 次 |
| 最近记录: |