Microsoft SQL四舍五入为整数,需要小数点后一位 [英] Microsoft SQL rounding off to whole number, need to 1 decimal place
问题描述
可能重复:
在SQL Server中截断(而不是舍入)小数位
无法弄清楚。当SQL舍入为整数时,我需要返回1个小数位。
Can't figure this out. I need 1 decimal place returned while SQL is rounding to whole numbers.
我读到整数除以整数就可以在SQL中得到整数,但是我需要在临时表中将输出值的小数点后一位保留下来。
I read that integers divided by integers give integers in SQL, but I need the one truncated decimal place for value of output in the temp table.
我不介意35.0返回35,但是35.17应该返回35.1。抱歉,刚刚编辑。需要截断最后一个数字,而不是四舍五入。
I don't mind if 35.0 comes back as 35 but 35.17 should come back as 35.1. Sorry just edited. Need to truncate the last number, not round up.
create table #blah(output decimal(9,1))
DECLARE @a money
DECLARE @b money
DECLARE @intinterval decimal(9,1)
SET @a = 5
SET @b = 2
SET @intinterval = (@b / 1000.0) * (86400.0 / @a)
INSERT INTO #blah (output) VALUES (@intinterval)
SELECT * from #blah
drop table #blah
以上等式应为(2/1000)*(86400/5)=(0.002 * 17280)= 34.56
The above equation should give (2 / 1000) * (86400 / 5) = (0.002 * 17280) = 34.56
34.56应该截断为34.5
The 34.56 should truncate to 34.5
推荐答案
SET @intinterval = cast(10 * (@b / 1000.0) * (86400.0 / @a) as int) / 10.0
或
SET @intinterval = cast(@b * 864.0 / @a as int) / 10.0
这篇关于Microsoft SQL四舍五入为整数,需要小数点后一位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!