2

I want to round to the nearest half decimals (geo coordinates) to do some data visualization. In t-sql, is there a built in function to round to half decimals (if that is the term). Examples of desired result:

    1.1 > 1.0
    1.4 > 1.5
    1.6 > 1.5
    1.9 > 2.0
Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
Roger
  • 2,063
  • 4
  • 32
  • 65
  • I don't know of any built-in function that does this. Can you give some additional examples like what does 1.25 and 1.3 round too? Maybe some one will know a simple way to do the math. – Adam Porad Mar 22 '12 at 21:55

1 Answers1

12

Just multiply by 2, round, and divide by 2.

select round(1.1 * 2, 0) / 2 -- > 1.0
select round(1.4 * 2, 0) / 2 -- > 1.5
select round(1.6 * 2, 0) / 2 -- > 1.5
select round(1.9 * 2, 0) / 2 -- > 2.0

Round on MSDN

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    @AdamPorad: `round(1.29 * 2) / 2` gives `1.5`. – Guffa Mar 22 '12 at 22:26
  • Thanks @Guffa and Thanks Tim Lehner for the example. I was rounding to 1 decimal place and then dividing. I'm still not sure how I did that math -- I must have done an extra rounding in somewhere. – Adam Porad Mar 23 '12 at 15:29