无法弄清楚这一点。当SQL舍入为整数时,我需要返回1位小数。
我读到整数除以整数给出了SQL中的整数,但是我需要在临时表中输出值的一个截断小数位。
我不介意35.0是35,但35.17会以35.1回归。抱歉刚刚编辑过。需要截断最后一个数字,而不是向上舍入。
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
34.56应截断为34.5
答案 0 :(得分:1)
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
答案 1 :(得分:0)
Round((@b / 1000.0) * (86400.0 / @a), 1, 1)
怎么样,最后1个说截断而不是圆。
答案 2 :(得分:0)
试试这个没有特殊功能......
如果
a = 5然后输出= 34.5(34.56)
a = 7输出= 24.6(24.69)
a = 11输出= 15.7(15.71)
create table #blah(output decimal(9,1))
DECLARE @a money
DECLARE @b money
DECLARE @intinterval decimal(9,2)
declare @rounded decimal(9,1)
declare @diff decimal(9,2)
declare @finalvalue decimal(9,1)
SET @a = 5
SET @b = 2
SET @intinterval = (@b / 1000.0) * (86400.0 / @a)
set @rounded = @intinterval -- gets the rounded value
set @diff = @intinterval - @rounded -- gets the difference whether to round off or not
if @diff >= 0 -- if differnce is >= 0 then get the rounded value .. eg. 34.31 = 34.3
set @finalvalue = @rounded
else -- else subtract 0.1 and get the rounded value e.g. 34.56 - 0.1 = 34.46 -> rounded value of 34.5
set @finalvalue = @intinterval - 0.1
INSERT INTO #blah (output) VALUES (@finalvalue )
SELECT * from #blah
drop table #blah