简体   繁体   English

T-SQL 四舍五入到小数位

[英]T-SQL round to decimal places

How do I round the result of matchpercent to two decimal places (%)?如何将 matchpercent 的结果四舍五入到两位小数 (%)? I'm using the following to return some results:我正在使用以下内容返回一些结果:

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 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. ...其中n是一个足够大的数字,不会截断小数点左侧。 That is to say, if you use "123.456", you need to use DECIMAL(7,2) because the total length is 7 digits.也就是说,如果使用“123.456”,则需要使用 DECIMAL(7,2),因为总长度为 7 位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM