StrugglingCoder
StrugglingCoder

Reputation: 5021

Why is my float displayed as 0 in postgreSQL

One of my columns process_size holds a value 800089856 in bytes.

My SQL query says

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.

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?

Upvotes: 0

Views: 1045

Answers (1)

user330315
user330315

Reputation:

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.

You should cast process_size to a float, then all subsequent operations are carried out using floats.

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.

Upvotes: 3

Related Questions