0

重複の可能性:
SQL Server の小数点以下の桁数を切り捨てる (丸めない)

これを理解することはできません。SQL が整数に丸められている間、小数点以下 1 桁を返す必要があります。

整数を整数で割ると SQL で整数が得られることを読みましたが、一時テーブルの出力の値には小数点以下 1 桁が切り捨てられる必要があります。

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 に切り詰める必要があります。

4

3 に答える 3

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
于 2012-05-17T03:52:28.003 に答える
0

についてはどうでしょうかRound((@b / 1000.0) * (86400.0 / @a), 1, 1)。最後の 1 は、丸めるのではなく切り捨てると言っています。

于 2012-05-17T05:10:15.213 に答える
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  
于 2012-05-17T20:43:47.303 に答える