简体   繁体   English

为什么我的float在postgreSQL中显示为0

[英]Why is my float displayed as 0 in postgreSQL

One of my columns process_size holds a value 800089856 in bytes . 我的其中一列process_size持有一个以bytes的值800089856

My SQL query says 我的SQL查询说

select ((process_size*11/(1024*1024*1024))*100)/(4*3600) as Avg_wk_sum from instances where wk_id = 2

But instead of a float value it shows 0 . 但是显示的不是0的浮点数。

I even tried explicit cast 我什至尝试了显式转换

select ((process_size*11/(1024*1024*1024))*100)/(4*3600) :: float as Avg_wk_sum from instances where wk_id = 2

What am I doing wrong? 我究竟做错了什么? How can I get the float value? 如何获得浮点值?

All values in your statement are integers, so the actual division/multiplication is carried out using integers - which yields 0. You then cast the result ( 0 ) to a float which doesn't change anything. 语句中的所有值都是整数,因此实际的除法/乘法是使用整数-产生0。然后将结果0 )强制转换为不改变任何内容的float。

You should cast process_size to a float, then all subsequent operations are carried out using floats. 您应将process_size转换为浮点数,然后使用浮点数执行所有后续操作。

select ((process_size::float*11/(1024*1024*1024))*100)/(4*3600)

However if you care about precise results, you should stay away from approximate data types like float and use numeric instead. 但是,如果您关心精确的结果,则应该远离近似数据类型(例如float),而应使用numeric

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM