Java 中紧凑的数字格式化行为(在十进制和科学记数法之间自动切换)

发布于 2024-12-15 15:37:54 字数 1306 浏览 2 评论 0原文

我正在寻找一种以标准十进制格式或科学记数法动态格式化浮点数的方法,具体取决于数字的值。

  • 对于中等大小,数字应格式化为小数,并抑制尾随零。如果浮点数等于整数值,则小数点也应该被抑制。
  • 对于极值(非常小或非常大),数字应以科学计数法表示。换句话说,如果标准十进制表示法的表达式中的字符数超过某个阈值,则切换为科学计数法。
  • 我应该控制精度的最大位数,但我不希望附加尾随零来表示最小精度;所有尾随零都应被抑制。

基本上,它应该针对紧凑性和可读性进行优化。

2.80000 -> 2.8

765.000000 -> 765

0.0073943162953 -> 0.00739432(在本例中将精度限制为 6)

0.0000073943162953 -> 7.39432E-6(如果幅度足够小,则切换到科学计数法 - 在本例中小于 1E-5

7394316295300000 -> 7.39432E+6(如果幅度足够大,则切换为科学计数法 - 例如,当大于 1E+10 时)

0.0000073900000000 -> ; 7.39E-6(从科学计数法中的有效数中去除尾随零)

0.000007299998344 -> 7.3E-6 (从 6 位精度限制舍入导致该数字的尾随零被删除)


这是我到目前为止发现的内容:

  • .toString() 方法可以完成我想要的大部分功能,但它不会在可能的情况下上转换为整数表示形式,并且不会以科学计数法表示大的整数值。另外,我不知道如何调整精度。
  • String.format(...) 函数的 "%G" 格式字符串允许我以可调整精度的科学记数法表示数字,但 不能去除尾随零

我想知道是否已经有一些库函数满足这些标准。我想我自己写这个的唯一障碍是必须从 %G 生成的科学记数法中的有效数中去除尾随零。

I am looking for a way to format a floating point number dynamically in either standard decimal format or scientific notation, depending on the value of the number.

  • For moderate magnitudes, the number should be formatted as a decimal with trailing zeros suppressed. If the floating point number is equal to an integral value, the decimal point should also be suppressed.
  • For extreme magnitudes (very small or very large), the number should be expressed in scientific notation. Alternately stated, if the number of characters in the expression as standard decimal notation exceeds a certain threshold, switch to scientific notation.
  • I should have control over the maximum number of digits of precision, but I don't want trailing zeros appended to express the minimum precision; all trailing zeros should be suppressed.

Basically, it should optimize for compactness and readability.

2.80000 -> 2.8

765.000000 -> 765

0.0073943162953 -> 0.00739432 (limit digits of precision—to 6 in this case)

0.0000073943162953 -> 7.39432E-6 (switch to scientific notation if the magnitude is small enough—less than 1E-5 in this case)

7394316295300000 -> 7.39432E+6 (switch to scientific notation if the magnitude is large enough—for example, when greater than 1E+10)

0.0000073900000000 -> 7.39E-6 (strip trailing zeros from significand in scientific notation)

0.000007299998344 -> 7.3E-6 (rounding from the 6-digit precision limit causes this number to have trailing zeros which are stripped)


Here's what I've found so far:

  • The .toString() method of the Number class does most of what I want, except it doesn't upconvert to integer representation when possible, and it will not express large integral magnitudes in scientific notation. Also, I'm not sure how to adjust the precision.
  • The "%G" format string to the String.format(...) function allows me to express numbers in scientific notation with adjustable precision, but does not strip trailing zeros.

I'm wondering if there's already some library function out there that meets these criteria. I guess the only stumbling block for writing this myself is having to strip the trailing zeros from the significand in scientific notation produced by %G.

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

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

发布评论

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

评论(1

浮世清欢 2024-12-22 15:37:54

您是否看过 DecimalFormat< /a> 还没有?

(也可能感兴趣: BigDecimal#toString())

Have you looked at DecimalFormat yet?

(what might also be of interest: BigDecimal#toString())

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