在圓形函式中,我們可以使用 (4,512,1),但對于 ceil 和 floor 則不然。我想將 ceil 和我的 floor 值 4,512 設定為 4,5 和 4,6,但小數點 1 并不總是恒定的。它可能會有所不同。對于帶小數點的樓層,可以使用 trunc 函式。有沒有辦法用小數點執行 ceil ?
uj5u.com熱心網友回復:
調整函式的round
定義方式(對于正數):
ROUND(n, integer) = FLOOR(n * POWER(10, integer) 0.5) * POWER(10, -integer)
...對于可變上限的一般情況,您可能需要以下內容:
ceil(n * power(10, integer)) * power(10, -integer)
所以你可以定義你自己的函式:
create or replace function my_ceil(p_number number, p_decimals pls_integer)
return number as
begin
return ceil(p_number * power(10, p_decimals)) * power(10, -p_decimals);
end;
/
create or replace function my_floor(p_number number, p_decimals pls_integer)
return number as
begin
return floor(p_number * power(10, p_decimals)) * power(10, -p_decimals);
end;
/
然后使用一些示例資料:
with t (n) as (
select 4.512 from dual union all
select 5.12345 from dual union all
select 6 from dual union all
select 0 from dual union all
select -1.23 from dual
)
select n, 0 as d, my_ceil(n, 0) as my_ceil, my_floor(n, 0) as my_floor from t
union all
select n, 1 as d, my_ceil(n, 1), my_floor(n, 1) from t
union all
select n, 2 as d, my_ceil(n, 2), my_floor(n, 2) from t
union all
select n, 3 as d, my_ceil(n, 3), my_floor(n, 3) from t
union all
select n, 4 as d, my_ceil(n, 4), my_floor(n, 4) from t
order by n, d
你得到:
? | D | MY_CEIL | MY_FLOOR |
---|---|---|---|
-1.23 | 0 | -1 | -2 |
-1.23 | 1 | -1.2 | -1.3 |
-1.23 | 2 | -1.23 | -1.23 |
-1.23 | 3 | -1.23 | -1.23 |
-1.23 | 4 | -1.23 | -1.23 |
0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 |
0 | 2 | 0 | 0 |
0 | 3 | 0 | 0 |
0 | 4 | 0 | 0 |
4.512 | 0 | 5 | 4 |
4.512 | 1 | 4.6 | 4.5 |
4.512 | 2 | 4.52 | 4.51 |
4.512 | 3 | 4.512 | 4.512 |
4.512 | 4 | 4.512 | 4.512 |
5.12345 | 0 | 6 | 5 |
5.12345 | 1 | 5.2 | 5.1 |
5.12345 | 2 | 5.13 | 5.12 |
5.12345 | 3 | 5.124 | 5.123 |
5.12345 | 4 | 5.1235 | 5.1234 |
6 | 0 | 6 | 6 |
6 | 1 | 6 | 6 |
6 | 2 | 6 | 6 |
6 | 3 | 6 | 6 |
6 | 4 | 6 | 6 |
db<>小提琴
您可能需要查看負值以檢查它們的行為是否符合您的預期/想要,并在round
必要時調整函式以模仿。您還說小數點可能為零、1 或更多;如果這可能是負面的,那么它將需要更多的作業......
uj5u.com熱心網友回復:
CEIL
并FLOOR
回傳最接近的整數并且不接受任何引數(但數字本身)。
所以,如果你想做到小數點后第 1 位,首先將數字乘以10
, CEIL
/FLOOR
該值,然后除以10
。
像這樣的東西:
SQL> with test (col) as (select 4.521 from dual)
2 select col,
3 --
4 ceil(col * 10) / 10 c_ceil,
5 floor(col * 10) / 10 c_floor
6 from test;
COL C_CEIL C_FLOOR
---------- ---------- ----------
4,521 4,6 4,5
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461887.html