Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL round function error for value 8.95

In SQL Server 2008:

declare @Value float
declare @result float 

set @Value=7.95
select @result=round(@Value,1)
print @result /*Prints 8*/

set @Value=8.95
select @result=round(@Value,1)
print @result /*Prints 8.9*/

The obtained result is 8.9 for 8.95, but if I put the value as 7.95 the result of the round function is 8.

Why do I get 8.9 instead of 9 for the value 8.95?

like image 990
Anoop Mohan Avatar asked May 10 '26 11:05

Anoop Mohan


1 Answers

Floating point (REAL and FLOAT) data is approximate. DECIMAL (NUMERIC) is exact: msdn.microsoft.com/en-us/library/ms187912(v=sql.105).aspx That might be the reason for this seemingly inconsistent behaviour.

like image 54
Ivan Golović Avatar answered May 13 '26 04:05

Ivan Golović