使用除法运算符时不了解 sql server 中的舍入行为 [英] Don't understand rounding behavior in sql server when using division operator

查看:41
本文介绍了使用除法运算符时不了解 sql server 中的舍入行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下下面的sql代码发生了什么?

Could anybody explain what happens in the following sql code?

声明@dividend numeric(38,22)

declare @dividend numeric(38,22)

声明@divisor numeric(38,22)

declare @divisor numeric(38,22)

声明@otherDivisor int

declare @otherDivisor int

设置@dividend = 1

set @dividend = 1

设置@divisor = 3

set @divisor = 3

设置@otherDivisor = 3

set @otherDivisor = 3

select cast(@dividend/@divisor as numeric(38,22)), @dividend/@otherDivisor

select cast(@dividend / @divisor as numeric(38,22)), @dividend / @otherDivisor

返回的结果是

0.3333330000000000000000 0.3333333333333333333333


0.3333330000000000000000 0.3333333333333333333333

我希望两种计算的结果相同.

I would expect the same result for both calculations.

推荐答案

decimal(38,22)/decimal(38,22) 以decimal(x, 6) 结尾,这些规则

decimal(38,22) / decimal(38,22) ends up with decimal(x, 6) following these rules

所以在转换回十进制(38,22)之前你有 0.33333

So you have 0.33333 before you cast back to decimal(38,22)

@otherDivisor 被转换为 (38, 0) 并保持为 decimal(x,22)

@otherDivisor is cast to (38, 0) and stays as decimal(x,22)

查看我的示例

这篇关于使用除法运算符时不了解 sql server 中的舍入行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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