SQL Server 2008:为什么不四舍五入到小数点后两位?
SELECT
ROUND(WeightInOZ / 16, 2)as WeightInLbs
FROM
Inventory
我得到的结果看起来像整数 1,2 等
SELECT
ROUND(WeightInOZ / 16, 2)as WeightInLbs
FROM
Inventory
The result I get looks like an integer 1,2 etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 16 更改为 16.0
您会看到奇怪的结果,因为它将除法结果视为整数而不是小数。指定
.0
告诉 sql server 将其视为十进制。更新:
如果尾随零让你感到害怕,你可以随时这样做:
Try changing 16 to 16.0
You are seeing strange results because it is treating the results of your division as an integer rather than a decimal. Specifying the
.0
tells sql server to treat it as a decimal.UPDATE:
If the trailing zero's are freaking you out you can always do this:
问题是这样的:
由于您正在处理两个整数,SQL Server 会截断余数,因此没有小数部分可供舍入。
您想要做的是强制它执行浮点(或十进制)除法。最简单的方法是将
16
更改为16.0
。The problem is this:
Since you're dealing with two integers, SQL Server truncates the remainder, so by there's no fractional component for it to round.
What you want to do is force it to perform floating-point (or decimal) division. The easiest way would be to change
16
to16.0
.