Re: T-SQL ROUND(decimal, int) vs C# Round(Decimal, Int32)



Matt (mattmorg55@xxxxxxxxx) writes:
> Anybody noticed that SQL Server rounds up if the value is half way
> between two rounded values, but C#'s Decimal.Round(Decimal,Int32)
> rounds to nearest even number?
>
>>>From MSDN: "When d is exactly halfway between two rounded values, the
> result is the rounded value that has an even digit in the far right
> decimal position. For example, when rounded to two decimals, the value
> 2.345 becomes 2.34 and the value 2.355 becomes 2.36. This process is
> known as rounding toward even, or rounding to nearest."

Unbelievable! I thought that this was just something that was taught
out of a whim for a while in Swedish schools when I was a kid. (Since
long replaced. A colleague who's 15 years younger than me was completely
baffled when she heard of the concept.)

> I perform the same calculation sometimes on the web server in C# and
> sometimes at the database in T-SQL, but want to get the same result
> from both calculations. Could anybody offer any strategies for dealing
> with this?

Write your own rounding function in any of the languages. If you are on
SQL 2000, it be may be better for performance to do it in C#. If you are
on SQL 2005, you would still do it in C#, but you could use the function
in SQL Server.

In fact, we have done something of the kind, although our problem was
different. We needed to round floating-point numbers "intelligently".

--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Query training -- Complex queries
    ... > Update PositionsEOM ... Here you don't have a derived table, but a correlated subquery. ... UPDATE in Books Online. ... is not written for SQL Server. ...
    (microsoft.public.sqlserver.server)
  • Re: Linked Server: How to check if server exists?
    ... The root problem is described in the Books Online topic Batches. ... Rick Byham, SQL Server Books Online ... linked server don't run on those development machines not having that linked ...
    (microsoft.public.sqlserver.connect)
  • Re: STORED PROCEDURE - passing table name as a parameter
    ... T-SQL, is that you get problems if the view definition does not fit into ... will need to query other system tables, for instance syscolumns. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Problem when changing Views
    ... There aree several reasons. ... I use neither of the tools, but I guess that Design View resubmits the ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: SQL 2005: Help files not valid?
    ... Please try the following instructions to remove and reinstall Books Online. ... If this doesn't solve the problem, we'll need to get the Visual Studio folks ... In Add or Remove Programs, select Microsoft SQL Server 2005, and then ... Complete the remaining steps in the Installation Wizard. ...
    (microsoft.public.sqlserver.setup)