简体   繁体   English

SQl中的舍入函数

[英]Round Function in SQl

select Round((10*100)/115,0) 选择舍入((10 * 100)/ 115,0)

Result : 8 结果:8

But Result : 8.69 但结果是8.69

But i want to display 9 但我想显示9

I tried below query also, but result is same.. 我也在下面的查询中尝试过,但是结果是一样的。

select Round((10*100)/115,0) 选择舍入((10 * 100)/ 115,0)

Please solve my prob.... 请解决我的问题。

Thanks 谢谢

Try below 试试下面

declare @n decimal(4,1)
select @n = 8.69

select case when PARSENAME(@n,1)>=5 then ceiling(@n) else floor(@n) end 

idea is if the number after point is greater than 5 then go to upper value else go to lower one 想法是如果点后的数字大于5,则转到较高的值,否则转到较低的一个

If your database is oracle then the following function you can use.. 如果您的数据库是oracle,则可以使用以下功能。

For result 9 use ceil function and For result 8 use floor function 结果9使用ceil函数,结果8使用floor函数

select ceil(8.69) val from dual;

  select floor(8.69) val from dual;

对于较高的值,您可以使用Ceil函数,对于较低的值,您可以在oracle中使用floor函数,如下所示

select ceil(8.69) Upper_val,floor(8.69) Lower_val from dual;

It returns an integer. 它返回一个整数。 Try to convert it to a decimal before calculation 尝试在计算之前将其转换为小数

Eg. 例如。

select Round((10*100)/115,0)

change to 改成

select Round((10.0*100)/115,0)

Hope this will help 希望这会有所帮助

If Its SQL Server please do try this: 如果其SQL Server请尝试这样做:

SELECT CEILING(10.1) AS UpperValue, FLOOR(10.6) AS LowerValue, 
Round(CAST((CAST(10 AS float) * CAST(100 AS float)) / CAST(115 AS float) AS float), 0)
AS YourValue, Round((10*100)/115,0) AS YourOldValue;

Here is what's happening: 这是正在发生的事情:

Even before using ROUND Function 甚至在使用ROUND函数之前

SELECT 1000/115 /* the result is 8 */

and try this 并尝试这个

SELECT ROUND(1000.0/15, 0) /* the result is 9.000000 */

and if use this 如果使用这个

SELECT CEILING(1000.0/115) /* the result is 9 */

so basically before call any function the result is 8. 所以基本上在调用任何函数之前,结果是8。

you can use this: 您可以使用此:

SELECT ROUND(CONVERT(decimal, 1000) / 115, 0)

or simply this: 或者只是这样:

SELECT ROUND(1000.0/15, 0)

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

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