PostgreSQL支持货币(money)类型,在内存中占用8 位空间,表示范围-92233720368547758.08 to +92233720368547758.07,有别于变精度浮点类型real 和 double precision,和numeric有些相似,都可以保证精度,区别在于货币类型会截取小数点后数据,有输出格式,输出格式受到lc_monetary设置影响。

#查看Linux系统lc_monetary
postgres=# show lc_monetary;
 lc_monetary
-------------
 zh_CN.UTF-8
(1 行记录)
#查看Windows系统lc_monetary,数据库版本10.0
test=# show lc_monetary;
                     lc_monetary
-----------------------------------------------------
 Chinese (Simplified)_People's Republic of China.936
(1 行记录)

test=#
---执行一个简单查询,提示:数据被截取显示
postgres=# select '111.333333'::money;
  money
----------
 ¥111.33
(1 行记录)

查看lc_monetary可支持设置类型。切换lc_monetary值不同查看结果。PostgreSQL默认值为C,支持本地化。

minmin@debian:~$ locale -a
C
C.UTF-8
POSIX
zh_CN.utf8
minmin@debian:~$
---切换至默认值
postgres=# set lc_monetary='C';
SET
postgres=#
postgres=#
postgres=# set lc_monetary='POSIX';
SET
postgres=#
postgres=# select '100.777'::money;
  money
---------
 $100.78
(1 行记录)

postgres=#

切换至POSIX以后,货币显示格式发生变化。

postgres=# set lc_monetary='C';
SET
postgres=# select '100.777'::money;
  money
---------
 $100.78
(1 行记录)

postgres=# set lc_monetary='zh_CN.utf8';
SET
postgres=# select '100.777'::money;
  money
----------
 ¥100.78
(1 行记录)

postgres=#

注意:money不包含币种信息,严格来讲不算货币数据类型,实际使用过程中还存在诸多不便,因此有人推荐使用decimal(numeric)数据类型。



PostgreSQL数据类型-货币类型Money插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://www.choupangxia.com/2020/06/28/postgresql-money/