postgreSQL保留小数
发布时间:2020-12-13 16:48:56 所属栏目:百科 来源:网络整理
导读:--1 例子 postgres=# select 1/4; ?column? ---------- 0 (1 row) 在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并且会截掉小数部分。 --2 类型转换 postgres=# select round(1::numeric/4::numeric,2); round ------- 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) 备注:类型转换后,就能保留小数部分了。 --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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- swift3.0 GCD DispatchSourceTimer注意事项
- 图像滤波和图像混合 Image Filtering and Hybrid Images
- c# – WPF功能区中的垂直分隔符
- objective-c – 在UINavigation Back按钮旁边添加一个UIBar
- PostgreSQL远程连接,发生致命错误:没有用于主机“…”,用
- c# – 对远程api的节流并行请求
- ruby-on-rails – 在gem Oauth rails的AccessToken post请求
- Oracle连接URL中的默认架构
- vb6 – 如何验证VB 6可执行文件的2个副本来自相同的代码库?
- SQLITE3中INSERT语句的注意事项