SQL Server 2000 Integer truncating

2019-03-02 02:02发布

Plain and simple, does anybody know why this:

Select 30 * 220 / 30

Returns 220, which is the correct result, and this:

Select 30 * (220/30)

Returns 210???

On the second case, I realise that 220/30 is being calculated first, generating a decimal (7,333333...) but still... isn't this lousy precision?

2条回答
Fickle 薄情
2楼-- · 2019-03-02 02:28

The one in the parentheses, is always evaluated first, but since the machine logic you are using integer, in that case, the result of the division is 7, wich you multiply by 30, gives you 210

查看更多
做自己的国王
3楼-- · 2019-03-02 02:35

Under integer division 220/30 = 7 and 99/100 = 0 (note truncation not rounding)

Use non integers to avoid this. e.g. Select 30 * (220/30.0)

Or you can use an explicit cast

Select 30 * (220/cast (30 as float))
查看更多
登录 后发表回答