dabestprogrammerindauniverse
asked on
Banker's Rounding
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.5->0
1.5->2
2.5->2
3.5->4
Thanks! :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i will try your solutions out and will then award my points :)
thanks to everyone.
thanks to everyone.
Hi dabestprogrammerindauniver se,
This function will round up or down to the degree you specify.
-- Round up to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVa rmoney,.01 ,1)
-- Round down to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVa rmoney,.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/@Roun dAmt THEN @Amt ELSE (CONVERT(BIGINT, @Amt/@RoundAmt)+@Direction )*@RoundAm t END)
END
Alan
This function will round up or down to the degree you specify.
-- Round up to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVa
-- Round down to the the nearest cent
-- Set @Somevarmoney = dbo.RoundToNearest(@SomeVa
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/@Roun
END
Alan
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.
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.
if
VAR-ROUND(VAR,0)=-0.5 (for 0.5 fractions)
if
FLOOR(VAR)/2=FLOOR(FLOOR(V
then
FLOOR(VAR),
else
ROUND(VAR,0,
endif
ROUND(VAR,0)
endif