简体   繁体   English

sql服务器回合

[英]sql server round

select round((cast(56/3 AS DECIMAL (4,2))),1)

is showing 18 output instead of 19 , where as actual value is 18.66. 显示的是18个输出而不是19个,实际值为18.66。 My Round function is not working please help. 我的回合功能不起作用,请帮忙。

The problem is 56/3 is an integer calculation which means no floating point numbers. 问题是56/3整数计算,这意味着没有浮点数。

You need to use floating point numbers in the initial calculation eg 56.0/3 . 您需要在初始计算中使用浮点数,例如56.0/3 Also, if you want to round to 19 then you need to round to the nearest whole number, ROUND(x, 1) will round to the first decimal place - you need to pass 0 to round up to 19. 另外,如果要舍入到19,则需要舍入到最接近的整数ROUND(x, 1)将舍入到小数点后第一位-您需要传递0以舍入到19。

SELECT ROUND((CAST(56.0/3 AS DECIMAL (4,2))),0)

Alternatively, you could switch ROUND for CEILING 或者,您可以将ROUND切换为CEILING

select CEILING(CAST(56.0/3 AS DECIMAL(4,2)))

Your section of the code: 您的代码部分:

CAST( 56/3 AS DECIMAL )

First evaluates the 56/3 which returns 18. This is then cast to decimal, giving 18.0. 首先评估返回值为18的56/3 。然后将其转换为十进制,得出18.0。

You need to cast either the numerator or denominator before the division occurs. 您需要在除法发生之前强制转换分子或分母。

The round function is working fine -- it's just not using the input you think it is. round功能运行良好-只是不使用您认为正确的输入。

Convert one of your integer values before you divide the numbers: 除以数字之前,请转换整数值之一:

select round(56/convert(DECIMAL (4,2),3),0);

If you do not so you divide integers which results in 18 not 18.66 如果不这样做,则将整数相除,结果为18而不是18.66

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM