PostgreSQLDBA(116)-pgAdmin(Don'tdothis:Don'tusemoney&serial)

no zuo no die系列,来自于pg的wiki。
这一节的内容是:不要使用money。
理由是:

money
It’s a fixed-point type, implemented as a machine int, so arithmetic with it is fast. But it doesn’t handle fractions of a cent (or equivalents in other currencies), it’s rounding behaviour is probably not what you want.
It doesn’t store a currency with the value, rather assuming that all money columns contain the currency specified by the database’s lc_monetary locale setting. If you change the lc_monetary setting for any reason, all money columns will contain the wrong value. That means that if you insert ‘$10.00’ while lc_monetary is set to ‘en_US.UTF-8’ the value you retrieve may be ‘10,00 Lei’ or ‘¥1,000’ if lc_monetary is changed.
Storing a value as a numeric, possibly with the currency being used in an adjacent column, might be better.

原因是money类型是定点类型,在计算机中通过机器int类型实现,取整行为可能不符合期望,同时该字段并没有存储货币单位,取决于数据库的lc_monetary设定,如果计量单位变化那么该值可能会存在问题,因此使用numeric存储数值额外使用其他字段存储货币单位。

创新互联建站主营化隆网站建设的网络公司,主营网站建设方案,重庆APP开发,化隆h5微信平台小程序开发搭建,化隆网站营销推广欢迎化隆等地区企业咨询

[local]:5432 pg12@testdb=# drop table if exists t_money;
NOTICE:  table "t_money" does not exist, skipping
DROP TABLE
Time: 35.404 ms
[local]:5432 pg12@testdb=# create table t_money(id int,c1 money);
CREATE TABLE
Time: 133.600 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(1,112343.01);
INSERT 0 1
Time: 0.929 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(2,112343.31234);
INSERT 0 1
Time: 0.640 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(3,112343.50);
INSERT 0 1
Time: 0.599 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(4,112343.99);
INSERT 0 1
Time: 0.523 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(5,112343.3199);
INSERT 0 1
Time: 0.483 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(6,112343.3150);
INSERT 0 1
Time: 0.533 ms
[local]:5432 pg12@testdb=# insert into t_money(id,c1) values(7,112343.3101);
INSERT 0 1
Time: 0.531 ms
[local]:5432 pg12@testdb=#

查询数据,实际的数据是四舍五入,保留两位小数

[local]:5432 pg12@testdb=# select * from t_money;
 id |     c1      
----+-------------
  1 | $112,343.01
  2 | $112,343.31
  3 | $112,343.50
  4 | $112,343.99
  5 | $112,343.32
  6 | $112,343.32
  7 | $112,343.31
(7 rows)
Time: 0.321 ms

修改数据库参数的货币设置为zh_CN

[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# show lc_monetary;
 lc_monetary 
-------------
 zh_CN.UTF-8
(1 row)
Time: 5.212 ms
[local]:5432 pg12@testdb=# select * from t_money;
 id |      c1      
----+--------------
  1 | ¥112,343.01
  2 | ¥112,343.31
  3 | ¥112,343.50
  4 | ¥112,343.99
  5 | ¥112,343.32
  6 | ¥112,343.32
  7 | ¥112,343.31
(7 rows)
Time: 2.092 ms
[local]:5432 pg12@testdb=#

美钞成了人民币。

参考资料
Don’t Do This


分享名称:PostgreSQLDBA(116)-pgAdmin(Don'tdothis:Don'tusemoney&serial)
转载来于:http://bzwzjz.com/article/gigpeg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆电商网站建设 定制网站制作 网站制作 广安网站设计 宜宾网站设计 成都网站设计 网站建设推广 重庆网站建设 网站建设改版 成都网站设计公司 成都网站建设公司 手机网站建设套餐 成都网站制作 重庆企业网站建设 网站建设公司 成都网站设计 成都网站设计 成都网站制作 成都网站制作 成都网站设计 企业网站设计 网站制作公司