Dat Truong
Dat Truong

Reputation: 13

SQL Server return different values if use a float type

In SQL Server 2012, i had a example:

DECLARE @baz float;
SET @baz = 7.19973;
select ROUND(@baz, 5, 1) --> 7.19972
select ROUND(7.19973, 5, 1) --> 7.19973

Please help me explain why the above query return to 7.19972 ? . And how to build a function like ROUND function on c# ?

Upvotes: 0

Views: 467

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269823

The issue is the third argument. Although the function is called round(), it actually truncates the value when the third argument is anything but 0. (See here.)

What is happening? A constant with a decimal point is a decimal value in SQL Server. The assignment to a float is approximate. In this case, the value must be something like: 7.19972999997. This is passed to round() as a float and the third argument causes the value to be truncated to 7.19972.

When you call:

select ROUND(7.19973, 5, 1)

The value is passed as a decimal. There is no conversion to float so the decimal values are exact and the truncation returns the original value.

Upvotes: 1

Related Questions