Getting rid of negative null value for float
With SQL Server 2008R2, I get a sign difference when rounding "the same" number as decimal or float.
Sample code showing the difference:
DECLARE @f float = -0.00001;
DECLARE @d decimal = -0.00001;
PRINT CONVERT(varchar,ROUND(@f, 4)) --- outputs -0
PRINT CONVERT(varchar,ROUND(@d, 4)) --- outputs 0
I need to round the float value to 0, not -0.
I think it boils down to the IEEE 754 specification, which defines two zero representations (positive and negative) whereas decimal numbers only have one zero. I think the float equality operator does the job, but I have to compare two AS floating point strings for compatibility reasons.
Is there a / function to get rid of negative zeros (at least when converting to a string)?
+3
Pascal
source
to share
2 answers
Adding 0.0 to the value fixed the problem. Thanks tmyblebu for the answer.
PRINT CONVERT(varchar,ROUND(@f + 0.0, 4))
+2
Pascal
source
to share
@Pascal Try below code. Let me know if it helped you.
DECLARE @f float = -0.00001;
DECLARE @d decimal = -0.00001;
PRINT abs(ROUND(@f, 4)) --- outputs 0
PRINT abs(ROUND(@f, 4)) --- outputs 0
-1
karan arora
source
to share