问题
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