2010-07-06 133 views
7

如何將matchpercent的結果舍入到小數點後兩位(%)? 我使用下面的返回了一些成果:T-SQL舍入到小數位

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 

回答

17

CAST/CONVERT結果:

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

...其中n是一個數字足夠大,以截斷離開小數點。也就是說,如果使用「123.456」,則需要使用DECIMAL(7,2),因爲總長度是7位數。