在 SQL Server 中四舍五入? [英] Rounding UP in SQL Server?

查看:44
本文介绍了在 SQL Server 中四舍五入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 sql server 编写了一个分页系统.效果很好,但我想返回总共有多少页

I have written a paging system for sql server. and it works great but i am trying to return how many pages there are in total

因此如果每页有 5 条记录和 2 条记录,那么总共是 3 页

Hence if there are 5 records and 2 records per page then the total is 3 pages

这就是我所拥有的

   SELECT @TotalPages = ( (SELECT COUNT(*) FROM #TempItems) / @RecsPerPage )

我的输出参数是这样定义的

and my output parameter is defined like so

   @TotalPages AS INT OUT,

现在它有点工作:-) 在我的测试中有 5 条记录和每页 2 条记录,所以上面的选择返回 2 但它的错误应该是 3

Now it sort of works :-) in my test there are 5 records and 2 records per page so the above select returns 2 but its wrong it should be 3

这是因为它说 5/2 = 整数 2...我该如何四舍五入...?

This is because its saying 5 / 2 = whole number 2... how do i round up...?

我累了天花板但无法让它工作..

I tired ceiling but couldn't get it to work..

有什么想法吗?

提前致谢

推荐答案

您是否尝试将分子和分母转换为浮点数,然后使用 Cieling?

Did you try Casting either the numerator and the denominator as float and then using Cieling?

整数运算总是给出整数.尝试以下 -

Integer operations always give integers. Try the following -

SELECT @TotalPages = CEILING((SELECT cast(COUNT(*) as float) FROM #TempItems) / @RecsPerPage ) 

这篇关于在 SQL Server 中四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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