四捨六入五成雙 BankersRound in SQL

摘要:四捨六入五成雙 BankersRound in SQL


CREATE FUNCTION dbo.BankersRound(@Val DECIMAL(32,16), @Digits INT)
RETURNS DECIMAL(32,16)
AS
BEGIN
    RETURN CASE WHEN ABS(@Val - ROUND(@Val, @Digits, 1)) * POWER(10, @Digits+1) = 5
            THEN ROUND(@Val, @Digits, CASE WHEN CONVERT(INT, ROUND(ABS(@Val) * POWER(10,@Digits), 0, 1)) % 2 = 1 THEN 0 ELSE 1 END)
            ELSE ROUND(@Val, @Digits)
        END
END

--select dbo.BankersRound(124.5, 0) 
-->124.00000000

 

http://www.chrispoulter.com/blog/entry/rounding-decimals-using-net-and-t-sql