一佳一

记录像1+1一样简洁的代码

导航

postgreSQL除法保留小数

Posted on 2023-01-17 18:03  一佳一  阅读(679)  评论(0编辑  收藏  举报
-1 例子
postgres=# select 1/4;
?column?
----------
0
(1 row)

在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并且会截掉小数部分。

--2 类型转换
postgres=# select round(1::numeric/4::numeric,2);
round
-------
0.25
(1 row)

备注:类型转换后,就能保留小数部分了。