So the banker's rounding algorithm must be applied from right to left? Try this:
create FUNCTION RoundBanker
( @Amtnumeric(38,16)
, @RoundToDecimal tinyint
)
RETURNS numeric(38,16)
AS
BEGIN
declare@RoundedAmtnumeric(38,16)
,@WholeAmtinteger
,@Decimaltinyint
,@Tennumeric(38,16)
set@Ten= 10.0
set@WholeAmt= ROUND(@Amt,0, 1 )
set@RoundedAmt= @Amt - @WholeAmt
set@Decimal= 16
While @Decimal > @RoundToDecimal
BEGIN
set @Decimal = @Decimal - 1
if 5 = ( ROUND(@RoundedAmt * POWER( @Ten, @Decimal + 1 ) ,0,1) - (ROUND(@RoundedAmt * POWER( @Ten, @Decimal ) ,0,1) * 10) )
and 0 = cast( ( ROUND(@RoundedAmt * POWER( @Ten, @Decimal ) ,0,1) - (ROUND(@RoundedAmt * POWER( @Ten, @Decimal - 1 ) ,0,1) * 10) ) AS INTEGER ) % 2
SET @RoundedAmt = ROUND(@RoundedAmt,@Decimal, 1 )
ELSE
SET @RoundedAmt = ROUND(@RoundedAmt,@Decimal, 0 )
END
RETURN ( @RoundedAmt + @WholeAmt )
END
GO
SQL = Scarcely Qualifies as a Language