0

Let's say a query like this

select (low + high)/2 'avg'
from tbl_values

when executed in SQL Server 2012 I get results like 166.65, but when I bind the same query to a C# datagridview it will be 166.64999999999998.

Why is SQL rounding when I didn't ask it to? And if it's by default, why isn't it shown as rounded in the datagridview?

For a given query, the executed result should be the same for SQL and C#... or not!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Katia
  • 679
  • 2
  • 15
  • 42
  • 1
    "why is SQL rounding " - Printing/Displaying is rounding.... – Mitch Wheat May 08 '14 at 06:48
  • should I post the canonical link? – Mitch Wheat May 08 '14 at 06:49
  • @mitch but why is 'printing' rounding to 2 digits after decimal point and to 3 in other records? I'm getting `166.65` and `286.335` .. yes please, links would be nice :> – Katia May 08 '14 at 06:51
  • 2
    Take a look at this [Full precision output of floating point types in SQL Server Management Studio](http://stackoverflow.com/questions/8099575/full-precision-output-of-floating-point-types-in-sql-server-management-studio). – scheien May 08 '14 at 07:03
  • Related (not necessarily a duplicate): *[T-SQL rounding vs. C# rounding](https://stackoverflow.com/questions/41592666)* – Peter Mortensen Jul 27 '21 at 16:09
  • @PeterMortensen I think your comment's place is on the other question since it's newer than this :) – Katia Jul 28 '21 at 07:35

0 Answers0