英文:
Azure Data Studio - My result is rounded - SQL
问题
请帮我,我不知道发生了什么事情,我的Azure Data Studio在计算时结果自动四舍五入。
在下面的示例中,结果是3.000而不是3.333,尽管我声明@Cute是十进制类型。
declare @Cute decimal(10,3) = 10/3
select @Cute
我上面已经提到了。
英文:
Please help me, I dont know what happened with my Azure Data Studio, when calculating, the result is automatedly rounded.
In the below example, the result is 3.000 not 3.333 even though I declared @Cute is in decimal type
declare @Cute decimal(10,3) = 10/3
select @Cute
I have mentioned above
答案1
得分: 1
以下是要翻译的内容:
变量的数据类型在这种情况下是无关紧要的,重要的是结果的目标 - 您正在使用隐式整数的文字值。
如果您至少定义一个参数为十进制类型,那么数据类型优先级将确保结果也是十进制的:
声明 @Cute decimal(10, 3) = 10.0 / 3;
选择 @Cute;
您还可以将已定义的变量用作参数之一,然后将适用相同的数据类型优先级,例如
声明 @Cute decimal(10, 3) = 10;
设置 @cute = @cute / 3;
选择 @Cute;
英文:
The data type of the variable is irrelevant in this case, it's the target of the result - you're using literal values which are implicitly integers.
If you define at least one of the arguments to be a decimal type then data-type precedence will ensure the result is also a decimal:
declare @Cute decimal(10, 3) = 10.0 / 3;
select @Cute;
You could also use the definied variable as one of the arguments, the same data type precedence then applies, such as
declare @Cute decimal(10, 3) = 10;
set @cute = @cute / 3;
select @Cute;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论