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



On 30 Jan 2006 08:42:43 -0800, Matt wrote:

>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."
>
>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?

Hi Matt,

This method of rounding is called "bankers rounding". Here's a link to a
previous discussion about it, including some techniques to do this in
SQL Server (warning - long URL, might wrap)

http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_frm/thread/b47c6ab25aacb7da/85e9e40e0a7f6f32?lnk=st&q=bankers+rounding+sql+server&rnum=1&hl=en#85e9e40e0a7f6f32

--
Hugo Kornelis, SQL Server MVP
.



Relevant Pages

  • Dates get rounded when passed to Sql Server with time component
    ... I used a very basic formula which ... I then passed this to an sql server parameter as its value for a query. ... the date is rounding in the first place between .net 2.0 and sql server. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Math.Round and SQL Server Round
    ... > I'm trying to determine the best approach for rounding in an application ... Unfortunately it appears as though SQL Server and VB.NET round ... To Cause VB to round in a normal way take the int of +.5 so for the numbers ... DECLARE @Num as Numeric ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Math.Round and SQL Server Round
    ... > I'm trying to determine the best approach for rounding in an application ... Unfortunately it appears as though SQL Server and VB.NET round ... To Cause VB to round in a normal way take the int of +.5 so for the numbers ... DECLARE @Num as Numeric ...
    (microsoft.public.sqlserver.programming)
  • Re: Dates get rounded when passed to Sql Server with time component
    ... I then passed this to an sql server parameter as its value for a query. ... the date is rounding in the first place between .net 2.0 and sql server. ...
    (microsoft.public.dotnet.framework.adonet)
  • T-SQL ROUND(decimal, int) vs C# Round(Decimal, Int32)
    ... Anybody noticed that SQL Server rounds up if the value is half way ... known as rounding toward even, or rounding to nearest." ... I perform the same calculation sometimes on the web server in C# and ...
    (comp.databases.ms-sqlserver)