Round in MS SQL on 0.05 or 0.00

前端 未结 2 1384
独厮守ぢ
独厮守ぢ 2021-01-27 09:11

Hello I am coming from Bosnia and Herzegovina and in our county the smallest note bill is 0.05, Now the government pushing us to our retrial prices rounding on 0.05 or at 0.00

2条回答
  •  长发绾君心
    2021-01-27 09:40

    There's nothing built-in - but it's easy enough to create:

    EDIT : adapted to your special rounding needs :-)

    create function dbo.RoundToFiveOrZero(@inputValue MONEY)
    returns MONEY
    as begin
      return FLOOR((@inputValue + 0.05) * 20.0) / 20.0
    end
    

    Use the DECIMAL or MONEY data type - whatever suits you best.

    Use it like this:

    select 
        dbo.RoundToFiveOrZero(1.51),
        dbo.RoundToFiveOrZero(1.53),
        dbo.RoundToFiveOrZero(7.33),
        dbo.RoundToFiveOrZero(7.37),
        dbo.RoundToFiveOrZero(7.39)
    

    Which gives you 1.50, 1.50, 7.30, 7.40, 7.40 as results.

    Marc

提交回复
热议问题