我得到不同货币的价格,要显示巴西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)
你能帮我吗?我应该只是删除过滤器?我应该强制执行一些正则表达式吗?我的网站的链接是http://www.koolbusiness.com/servead/4252196
UPDATE:我使用的是像这些过滤器的一个考虑:
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)
“没有工作”不是很有帮助。当你尝试时发生了什么? –
作为一个字符串销售每盎司的清单,可能还有其他我应该承认的组合。我没有想到一个正则表达式或规则,不会让某人不可能出售例如金属每盎司 –