Oracle显示号码

发布于 2025-01-30 08:52:37 字数 155 浏览 1 评论 0原文

我们需要采用一些非常大的数字,并以这种缩写的方式显示它们:

2113546998.37   --> 21.37B
15481063.31  --> 15.31M

依此类推。我认为甲骨文没有这样做的方法。希望提供一些帮助。

We need to take some very large numbers and display them in an abbreviated fashion like this:

2113546998.37   --> 21.37B
15481063.31  --> 15.31M

And so on. I do not think Oracle has a method for doing this. Was hoping for some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2025-02-06 08:52:37

您可以使用日志和力量来操纵和解释价值;要将其四舍五入到十进制位置,因为它是最近的“大数字”括号:

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

依此类推,尽管您要大于此,则必须决定如何区分四周和Quintillion。

样本数据,完整查询:

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

获取

您的_number结果
123.456789123.46
1234.567891.23K
12345.678912.35K
123456.789123.46K 123.46K 1234567.89
B123.46T
123456789123456123456789123
15.48123.46b
获得因此
有了一些扩展的
2.11b

和 M对于您的两个原始值,而不是21.37b
正如您的问题所示,15.31m - 但正如评论中指出的那样,只保留两个极端的精确度并没有意义。当然,可以做到这一点 - 地板而不是圆,并附加原始小数部分 - 但似乎这不太可能是您真正的意思,我认为21 vs 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.461234.56789
1234.5712345.6789
12345.68123456.789
123456.791234567.89
789123都可以轻松地
哪种123.46b
123456789123456
your_number
方式,
123.46T无论

逻辑放入函数中

00afdle = 00af7ae30fcfd6d35f5d7ce9514329d7” ;如果您需要处理零或负数,则需要

You could use log and power to manipulate and interpret the value; to get it rounded to decimal places for it's nearest 'large number' bracket:

round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)

And then to get the letter to append, something like:

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

and so on, though if you get larger than that you'll have to decide how to distinguish between quadrillion and quintillion.

With some extended sample data, a full query of:

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

gets

YOUR_NUMBERRESULT
123.456789123.46
1234.567891.23K
12345.678912.35K
123456.789123.46K
1234567.891.23M
15481063.3115.48M
123456789123.46M
2113546998.372.11B
123456789123123.46B
123456789123456123.46T

So that gets 2.11B and 15.48M for your two original values, not 21.37B
and 15.31M as your question showed - but as pointed out in comments, it wouldn't really make sense to only keep both extremes of the precision. It's possible do do that, of course - floor instead of round, and append the original decimal part - but it seems unlikely that's what you really meant, and I've assume both 21 vs 2 and the decimal parts are mistakes putting the question together.

You might not want to apply it to smaller numbers though - 'K' is perhaps less common? - and if so you could use another case expression to decide. For example:

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
YOUR_NUMBERRESULT
123.456789123.46
1234.567891234.57
12345.678912345.68
123456.789123456.79
1234567.891.23M
15481063.3115.48M
123456789123.46M
2113546998.372.11B
123456789123123.46B
123456789123456123.46T

Either way you can easily put the logic into a function.

db<>fiddle

I've only looked at positive, non-zero numbers; if you need to handle zero or negative numbers then it will need a little bit more work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文