java - 如何将 BigDecimal 转换为指数形式?

标签 java bigdecimal number-formatting exponential

如何将 BigDecimal 对象转换为使用指数形式的字符串表示形式?类似于:3.134e67?我查看了 API 并找到了 toEngineeringString(),但它没有给我想要的东西。

最佳答案

你读过 NumberFormat 文档了吗,这是来自 DecimalFormat: 参见:http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html

Scientific Notation

Numbers in scientific notation are expressed as the product of a mantissa and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The mantissa is often in the range 1.0 <= x < 10.0, but it need not be. DecimalFormat can be instructed to format and parse scientific notation only via a pattern; there is currently no factory method that creates a scientific notation format. In a pattern, the exponent character immediately followed by one or more digit characters indicates scientific notation. Example: "0.###E0" formats the number 1234 as "1.234E3".

  • The number of digit characters after the exponent character gives the minimum exponent digit count. There is no maximum. Negative exponents are formatted using the localized minus sign, not the prefix and suffix from the pattern. This allows patterns such as "0.###E0 m/s".

  • The minimum and maximum number of integer digits are interpreted together:

    • If the maximum number of integer digits is greater than their minimum number and greater than 1, it forces the exponent to be a multiple of the maximum number of integer digits, and the minimum number of integer digits to be interpreted as 1. The most common use of this is to generate engineering notation, in which the exponent is a multiple of three, e.g., "##0.#####E0". Using this pattern, the number 12345 formats to "12.345E3", and 123456 formats to "123.456E3".

    • Otherwise, the minimum number of integer digits is achieved by adjusting the exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4".

  • The number of significant digits in the mantissa is the sum of the minimum integer and maximum fraction digits, and is unaffected by the maximum integer digits. For example, 12345 formatted with "##0.##E0" is "12.3E3". To show all digits, set the significant digits count to zero. The number of significant digits does not affect parsing.

  • Exponential patterns may not contain grouping separators.

关于java - 如何将 BigDecimal 转换为指数形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9370278/

相关文章:

java - 在 swing 中单击按钮时显示 JfreeChart

java - 在 Java 中四舍五入到最接近的 0.05 的最佳方法

java - 使用 Map 填充 Java 对象

java - Hibernate 错误地映射 BigDecimal

google-sheets - 转换电子表格十进制数字格式 : period- and comma-separated decimal positions

java - 星算法不起作用

java - 通过Java以某种方式输出文件

excel - 如何确保 Excel 不会更改其他语言版本中数据标签的数字格式?

string - as.numeric 带有逗号小数分隔符?

java - 如何从xml中引用图像?