February 17, 2010 at 4:32 am
Predict output for ROUND function
This is the QotD in SQL Server Central only. I did not understand the solution. Could anyone elaborate it.
What are the outputs?
DECLARE @round1 float
SET @round1 = 165.755
SELECT ROUND(@round1,2)
DECLARE @round2 float
SET @round2 = 165.555
SELECT ROUND(@round2,2)
Correct answer:
165.75 and 165.56
Explanation:
A float, without the size defined , is defaulted to a float(53), which is an 8 byte double precision. If I change the variables to anything from float(25) to float(53), I get the wrong answer (75.42). If i use flat(24) or less, I get the correct answer.
Use CAST operator to get proper the results. Example: SELECT ROUND(CAST(@round1 AS DECIMAL(15,3)),2)
And again if we try this,
DECLARE @round1 float(25)
SET @round1 = 165.7555
SELECT ROUND(@round1,3)
DECLARE @round2 float(25)
SET @round2 = 165.5555
SELECT ROUND(@round2,3)
The answer is 165.756, 165.555
February 19, 2010 at 12:31 pm
I want to say that this is because of the repeating '5' three times.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy