加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL数据类型-货币类型

发布时间:2020-12-13 16:45:45 所属栏目:百科 来源:网络整理
导读:PostgreSQL支持货币(money)类型,在内存中占用8 位空间,表示范围-92233720368547758.08 to +92233720368547758.07,有别于变精度浮点类型real 和 double precision,和numeric有些相似,都可以保证精度,区别在于货币类型会截取小数点后数据,有输出格式

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读