Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

T-SQL ROUND(decimal, int) vs C# Round(Decimal, Int32)
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?

Thanks ~ Matt


Report this thread to moderator Post Follow-up to this message
Old Post
Matt
01-30-06 04:25 PM


Re: T-SQL ROUND(decimal, int) vs C# Round(Decimal, Int32)
I would be interested to hear what the veterans in this group have to
say about this one also.  I use Report Builder for our reports and
forms and it uses Delphi Pascal.  This variant of Pascal uses the
rounding to even also.  Makes for interesting reports when SQL does one
side of the equation (like a subtotal + tax + shipping) and Pascal does
the grand total, it's quite possible to end up with 100.01 + 0 + 0 =
100.00 (SQL value + SQL value + SQL value = Pascal value)

Gabe


Report this thread to moderator Post Follow-up to this message
Old Post
gabe101
01-31-06 01:24 AM


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?
> 
>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/micr...0a7f6f
32

--
Hugo Kornelis, SQL Server MVP

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
01-31-06 01:24 AM


Re: T-SQL ROUND(decimal, int) vs C# Round(Decimal, Int32)
Matt (mattmorg55@gmail.com)  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?
> 
> 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@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
01-31-06 01:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 05:20 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006