簡體   English   中英

T-SQL 四舍五入到小數位

[英]T-SQL round to decimal places

如何將 matchpercent 的結果四舍五入到兩位小數 (%)? 我正在使用以下內容返回一些結果:

DECLARE @topRank int
set @topRank=(SELECT MAX(RANK) FROM
FREETEXTTABLE(titles, notes, 'recipe cuisine', 1))
SELECT 
    ftt.RANK, 
    (CAST(ftt.RANK as DECIMAL)/@topRank) as matchpercent, --Round this
    titles.title_id, 
    titles.title
FROM Titles
INNER JOIN 
FREETEXTTABLE(titles, notes, 'recipe cuisine') as ftt
ON
ftt.[KEY]=titles.title_id
ORDER BY ftt.RANK DESC

CAST/CONVERT 結果:

CAST((CAST(ftt.RANK as DECIMAL)/@topRank) AS DECIMAL(n,2)) as matchpercent,

...其中n是一個足夠大的數字,不會截斷小數點左側。 也就是說,如果使用“123.456”,則需要使用 DECIMAL(7,2),因為總長度為 7 位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM