Python和Postgres区域设置货币格式之间的区别

时间:2013-04-08 15:01:53

标签: python postgresql locale currency

Python 2.6:

import locale
locale.setlocale(locale.LC_MONETARY, 'fr_CA.UTF-8')
locale.currency(1.234) # '1,23 $'

Postgres 9.1:

set lc_monetary = 'fr_CA.UTF-8';
select 1.234::money; -- '$1,23'

我认为Python版本是正确的(虽然不是100%肯定),但它们怎么可能不同呢?

2 个答案:

答案 0 :(得分:1)

加拿大有两种货币格式(在这方面它们可能是独一无二的)。有英语 - 加拿大(en_CA)和法语 - 加拿大(fr_CA)。

由于我的理解有限,Python的格式对于法语 - 加拿大人来说是正确的,而PostgreSQL似乎使用英语 - 加拿大语。

PostgreSQL中的货币支持完全有可能只允许每个国家/地区使用一种格式。多年来,它没有得到很多关注和关注,大多数人使用数字进行财务计算,然后让客户端代码处理格式化。

如果对您来说很重要,可能值得发布错误报告(甚至补丁)。

答案 1 :(得分:0)

正如Tom Lane在PG-general邮件列表中所报道的那样,这个问题已在9.2中修复:

Support more locale-specific formatting options for the money
data type (Tom Lane)

Specifically, honor all the POSIX options for ordering of the
value, sign, and currency symbol in monetary output. Also, make
sure that the thousands separator is only inserted to the left
of the decimal point, as required by POSIX.

我在OSX上尝试使用9.2.4,确实有效。