T-SQL舍入到小数位

时间:2021-12-05 16:31:06

How do I round the result of matchpercent to two decimal places (%)? I'm using the following to return some results:

如何将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

1 个解决方案

#1


17  

CAST/CONVERT the result:

CAST / CONVERT结果:

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

...where n is a number large enough not to truncate left of the decimal point. That is to say, if you use "123.456", you need to use DECIMAL(7,2) because the total length is 7 digits.

...其中n是一个足够大的数字,不会截断小数点的左边。也就是说,如果使用“123.456”,则需要使用DECIMAL(7,2),因为总长度为7位。

#1


17  

CAST/CONVERT the result:

CAST / CONVERT结果:

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

...where n is a number large enough not to truncate left of the decimal point. That is to say, if you use "123.456", you need to use DECIMAL(7,2) because the total length is 7 digits.

...其中n是一个足够大的数字,不会截断小数点的左边。也就是说,如果使用“123.456”,则需要使用DECIMAL(7,2),因为总长度为7位。