[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Banker's Rounding

Posted on 2004-10-03
11
Medium Priority
?
1,010 Views
Last Modified: 2008-02-01
MS SQL Server has the Round function that rounds-off of mid-values (0.5, 5, 50, 500, etc.) up. Is there a function that will round-off mid-values according to Banker's Rounding, which will round of mid-values always to the even value? Anyone has one?
0.5->0
1.5->2
2.5->2
3.5->4
Thanks! :)
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Accepted Solution

by:
tomcat75 earned 172 total points
ID: 12214982
this is for Oracle
but for MSSQL  use the same logic (usinga i think CASE ...)

DECODE(VAR-ROUND(VAR,0),-0.5,DECODE(FLOOR(VAR)/2, FLOOR(FLOOR(VAR)/2),FLOOR(VAR),ROUND(VAR,0)),ROUND(VAR,0))

HTH
0
 
LVL 1

Expert Comment

by:tomcat75
ID: 12215003
maybe an explanation will help

if
VAR-ROUND(VAR,0)=-0.5 (for 0.5 fractions)
if
FLOOR(VAR)/2=FLOOR(FLOOR(VAR)/2) --the number is even
then
FLOOR(VAR),
else
ROUND(VAR,0,
endif
ROUND(VAR,0)
endif


0
 
LVL 5

Assisted Solution

by:kemp_a
kemp_a earned 164 total points
ID: 12215025
Hmm, funny, most MS development products used Bankers Rounding, some of the applications like Excel dont, but thats another story.

You can emulate Banker Rounding using the following formula:

round((Value*100-Half of the rounding value)/100)

eg

round(12.5*100-0.05)/100=12 or

Cheers
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 1

Assisted Solution

by:xabimond
xabimond earned 164 total points
ID: 12215706
Encapsulated in a searched case:

declare @d as decimal(10,2)
set @d = 1.5
print @d

select (case when @d - round(@d, 0) <> -.5 then round(@d, 0)
      when (convert(int, floor(@d)) % 2) = 0 then floor(@d)
      else ceiling(@d)
      end) banker_rounded_number

Replace @d with your field name if appropriate.
0
 

Author Comment

by:dabestprogrammerindauniverse
ID: 12336432
i will try your solutions out and will then award my points :)
thanks to everyone.
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 12441471
Hi dabestprogrammerindauniverse,

This function will round up or down to the degree you specify.

-- Round up to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVarmoney,.01,1)

-- Round down to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVarmoney,.01,0)


CREATE Function [dbo].[RoundToNearest] (@Amt decimal(38, 19), @RoundAmt decimal(38, 19), @Direction bit)
-- @Direction=1 for RoundUp, @Direction=0 for RoundDown
RETURNS decimal(38, 19) AS  
BEGIN
  RETURN (CASE WHEN CONVERT(BIGINT, @Amt/@RoundAmt)=@Amt/@RoundAmt THEN @Amt ELSE (CONVERT(BIGINT, @Amt/@RoundAmt)+@Direction)*@RoundAmt END)
END


Alan
0
 
LVL 5

Expert Comment

by:kemp_a
ID: 12748030
May I ask the reason for this recommendation.

Not discounting alanwarren's solution, which appears in essence very similar to the three previously posted solutions albeit posted 25 days after those solutions, and indeed 11 days after the asker posted they would try the already proffered solutions.

I believe the point should be shared between Tomcat75, xabimond & myself as these were posted prior to the askers final post and indeed in good hast to the askers question.

I look forward to your reply.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question