Home Forums SQL Server 7,2000 T-SQL Banker''s rounding in T-SQL (like Math.Round in .NET) RE: Banker''''s rounding in T-SQL (like Math.Round in .NET)

  • Andrew... This also works... and, it's set based.... just substitute a column name for @Number in the formula...

    -- Math.Round(3.454,2) -> 3.45

    -- Math.Round(3.455,2) -> 3.46

    -- Math.Round(3.445,2) -> 3.44

    -- Math.Round(3.456,2) -> 3.46

    DECLARE @Number MONEY

    DECLARE @precision INT

        SET @precision = 2

       SET @Number = 3.454

    SELECT ROUND(@Number,@Precision,CAST(@Number*POWER(10,@Precision) AS INT)%2)

       SET @Number = 3.455

    SELECT ROUND(@Number,@Precision,CAST(@Number*POWER(10,@Precision) AS INT)%2)

       SET @Number = 3.445

    SELECT ROUND(@Number,@Precision,CAST(@Number*POWER(10,@Precision) AS INT)%2)

       SET @Number = 3.456

    SELECT ROUND(@Number,@Precision,CAST(@Number*POWER(10,@Precision) AS INT)%2)

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)