问题描述
我正在寻找一种根据数字的值以标准十进制格式或科学记数法动态格式化浮点数的方法.
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
(限制精度位数——在这种情况下为 6)
0.0073943162953
-> 0.00739432
(limit digits of precisionto 6 in this case)
0.0000073943162953
-> 7.39432E-6
(如果幅度足够小,则切换到科学记数法——这里小于1E-5
案例)
0.0000073943162953
-> 7.39432E-6
(switch to scientific notation if the magnitude is small enoughless than 1E-5
in this case)
7394316295300000
-> 7.39432E+6
(如果幅度足够大,则切换到科学记数法——例如,当大于1E+10
代码>)
7394316295300000
-> 7.39432E+6
(switch to scientific notation if the magnitude is large enoughfor example, when greater than 1E+10
)
0.0000073900000000
-> 7.39E-6
(用科学记数法去除有效数的尾随零)
0.0000073900000000
-> 7.39E-6
(strip trailing zeros from significand in scientific notation)
0.000007299998344
-> 7.3E-6
(从 6 位精度限制四舍五入导致此数字的尾随零被剥离)
0.000007299998344
-> 7.3E-6
(rounding from the 6-digit precision limit causes this number to have trailing zeros which are stripped)
这是我目前发现的:
- Number 类的
.toString()
方法完成了我想要的大部分工作,只是它在可能的情况下不会上转换为整数表示,并且不会以科学记数法表示大的整数幅度.另外,我不确定如何调整精度. "%G"
格式字符串到String.format(...)
函数允许我以可调节的精度以科学记数法表示数字,但是 不去除尾随零.
- 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 theString.format(...)
function allows me to express numbers in scientific notation with adjustable precision, but does not strip trailing zeros.
我想知道是否已经有一些库函数符合这些标准.我想我自己写这个的唯一绊脚石是必须从 %G
生成的科学记数法中的有效数中去除尾随零.
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
.
推荐答案
你看过 DecimalFormat 了吗?
(可能还有兴趣:BigDecimal#toString())
这篇关于Java 中的紧凑数字格式化行为(在十进制和科学记数法之间自动切换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!