Casting '0' to double precision, then multiple by -1 will result in negative zero

692 Views Asked by At

I'm trying to negate a double precision value inside jsonb data. I had to cast it to double since PG will return it as text. I found that zero value are printed as -0

Is that fine? how to fix it without "CASE WHEN"

enter image description here

2

There are 2 best solutions below

0
jjanes On

If you add 0.0 to the result, it will normalize it to regular 0.

2
Belayer On

As @Bergi asked: What is the problem? The properties of 0 and -0 are exactly the same. Run:

select '0 vs. -0. they are ' ||
       case when ( 0 = -0) 
            then 'Equal'
            else 'Not Equal'
       end;

Also see demo here. In all cases the results are the same. Multiplication produces -0 instead of 0, but since they are the same they can be freely substituted.