postgreSQL保留小数

前端之家收集整理的这篇文章主要介绍了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)--1 例子 postgres=# select 1/4; ?column? ---------- 0 (1 row) 在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并且会截掉小数部分。 --2 类型转换 postgres=# select round(1::numeric/4::numeric,1); substr -------- 3 (1 row) 原文链接:https://www.f2er.com/postgresql/193602.html

猜你在找的Postgre SQL相关文章