postgreSQL除法保留小数

postgreSQL除法保留小数,第1张

概述--1 例子 postgres=# select 1/4;  ?column?  ----------         0 (1 row)         在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并且会截掉小数部分。   --2 类型转换 postgres=# select round(1::numeric/4::numeric,2);  round  --1 例子
postgres=# select 1/4;
 ?column? 
----------
        0
(1 row)

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

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

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


--3 也可以通过 cast 函数进行转换
postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);
 round 
-------
  0.25
(1 row)

--4 关于 cast 函数的用法postgres=# SELECT substr(CAST (1234 AS text),3,1); substr -------- 3(1 row)

总结

以上是内存溢出为你收集整理的postgreSQL除法保留小数全部内容,希望文章能够帮你解决postgreSQL除法保留小数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1163569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存