TSQL 四舍五入到小数点后半
问题描述:
我想四舍五入到最接近的小数点(地理坐标)以进行一些数据可视化.在 t-sql 中,是否有一个内置函数可以四舍五入到小数点的一半(如果是这个词的话).所需结果示例:
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
答
只需乘以 2,取整,再除以 2.
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