PostgreSQL数据类型-货币类型

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,支持本地化。post

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之后,货币显示格式发生变化。code

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)数据类型。内存

相关文章
相关标签/搜索