Getting the ceil value of a number in SQLite

穿精又带淫゛_ 提交于 2021-02-07 12:45:57

问题


So I see this question has a good answer, but I want to round up no matter what, instead of rounding down no matter what. Adding 1 to it before casting int wouldn't work because it would "round" 5.0 into 6.0, for example.

So how should I implement ceil in SQLite?


回答1:


How about this?

select (case when x = cast(x as int) then cast(x as int)
             else 1 + cast(x as int)
        end)



回答2:


This will give you the same answer more elegantly:

SELECT CAST(x+1-1e-n AS INT);

(assuming you won't have a precision greater than n decimal points)




回答3:


using php you can easily:

$db->createFunction('ceil', 'ceil', 1);

$db->createFunction('floor', 'floor', 1);

select ceil(\`column`) from table;



回答4:


Some useful answers, I found this solution really easy as well.

select round(column+0.5) from table;

It will mean you are always rounding up if your original number is < 0.5 or rounding back down if your original number is > 0.5



来源:https://stackoverflow.com/questions/14969067/getting-the-ceil-value-of-a-number-in-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!