postgreSQL除法保留小数

Posted telwanggs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgreSQL除法保留小数相关的知识,希望对你有一定的参考价值。

--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保留小数

怎样在c语言除法中保留小数点?

android中如何实现除法的保留小数点后2位,四舍五入!

linux 除法保留小数

WPS 表格怎么做除法?并保留小数一位?

excel2007除法结果为啥只有整数,怎样才能保留两位小数?