T-SQL 四舍五入到小数位 [英] T-SQL round to decimal places

查看:31
本文介绍了T-SQL 四舍五入到小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 matchpercent 的结果四舍五入到两位小数 (%)?我正在使用以下内容返回一些结果:

How do I round the result of matchpercent to two decimal places (%)? 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 结果:

CAST/CONVERT the result:

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

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

...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.

这篇关于T-SQL 四舍五入到小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆