我們需要獲取一些非常大的數字并以如下所示的縮寫方式顯示它們:
2113546998.37 --> 21.37B
15481063.31 --> 15.31M
等等。我不認為 Oracle 有這樣做的方法。希望得到一些幫助。
uj5u.com熱心網友回復:
你可以使用 log 和 power 來操縱和解釋值;將其四舍五入到最接近的“大數”括號的小數位:
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
然后要附加這封信,例如:
case 3 * floor(log(10, your_number) / 3)
when 0 then null when 3 then 'K' when 6 then 'M'
when 9 then 'B' when 12 then 'T' when 15 then 'Q'
end
依此類推,但如果你變得比這更大,你將不得不決定如何區分千萬億和千億。
使用一些擴展的樣本資料,一個完整的查詢:
select your_number,
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
||
case 3 * floor(log(10, your_number) / 3)
when 0 then null when 3 then 'K' when 6 then 'M'
when 9 then 'B' when 12 then 'T' when 15 then 'Q'
else 'x'
end as result
from your_table
order by your_number
得到
你的電話號碼 | 結果 |
---|---|
123.456789 | 123.46 |
1234.56789 | 1.23K |
12345.6789 | 12.35K |
123456.789 | 123.46K |
1234567.89 | 1.23M |
15481063.31 | 15.48M |
123456789 | 123.46M |
2113546998.37 | 2.11B |
123456789123 | 123.46B |
123456789123456 | 123.46T |
因此,您的兩個原始值分別為 2.11B 和 15.48M,而不是您的問題顯示的 21.37B 和 15.31M - 但正如評論中指出的那樣,只保持兩個極端的精度是沒有意義的。當然,可以這樣做 - 地板而不是圓形,并附加原始的小數部分 - 但這似乎不太可能是你真正的意思,我假設 21 對 2 和小數部分都是錯誤的把問題放在一起.
不過,您可能不想將其應用于較小的數字 - “K”可能不太常見?- 如果是這樣,您可以使用另一個案例運算式來決定。例如:
select your_number,
case
when log(10, your_number) < 6
then to_char(round(your_number, 2))
else
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
||
case 3 * floor(log(10, your_number) / 3)
when 6 then 'M' when 9 then 'B' when 12 then 'T' when 15 then 'Q'
else 'x'
end
end as result
from your_table
order by your_number
你的電話號碼 | 結果 |
---|---|
123.456789 | 123.46 |
1234.56789 | 1234.57 |
12345.6789 | 12345.68 |
123456.789 | 123456.79 |
1234567.89 | 1.23M |
15481063.31 | 15.48M |
123456789 | 123.46M |
2113546998.37 | 2.11B |
123456789123 | 123.46B |
123456789123456 | 123.46T |
無論哪種方式,您都可以輕松地將邏輯放入函式中。
db<>小提琴
我只查看了正數、非零數;如果您需要處理零或負數,則需要更多的作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478825.html
上一篇:根據某人出生的月份進行過濾