我想在sql中能够将数字转化为n个重要数字.所以:
123.456 rounded to 2sf would give 120 0.00123 rounded to 2sf would give 0.0012
我知道ROUND()函数,它返回到n个小数位,而不是有效数字.
解决方法
选择round(@ number,@ sf-1- floor(log10(abs(@number))))应该做的伎俩!
成功测试了你的两个例子.
编辑:在@ number = 0上调用此函数将不起作用.在使用此代码之前,您应该添加一个测试.
create function sfround(@number float,@sf int) returns float as begin declare @r float select @r = case when @number = 0 then 0 else round(@number,@sf -1-floor(log10(abs(@number )))) end return (@r) end