java - 使用固定小数位数的工程符号格式 double

标签 java format

我想用 DecimalFormat以工程符号(指数是 3 的倍数)和固定数量的小数位来格式化数字。小数位数应由模式配置。

使用 java.text.DecimalFormat 可以吗?类(class)。
有替代方案吗?

这是输出 12.345E3 而不是 12.34E3 的测试用例:

public static void main(String[] args)
{
    Locale.setDefault(Locale.ENGLISH);
    DecimalFormat df = new DecimalFormat("##0.00E0");

    String realOutput = df.format(12345);
    String expected = "12.34E3";

    System.out.println(realOutput);
    if (Objects.equals(realOutput, expected))
    {
        System.out.println("OK");
    }
    else
    {
        System.err.println("Formatted output " + realOutput + " differs from documented output " + expected);
    }
}

最佳答案

您应该使用以下内容:

DecimalFormat format = new DecimalFormat("##0.0E0");

这里 http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

您可以在“科学符号”中找到有关您问题的答案

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 - 使用固定小数位数的工程符号格式 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679136/

相关文章:

Python Xlsxwriter - 使日期尊重不同的区域设置格式

c - 为什么 C 没有无符号 float ?

java - Spring Boot - 大量无效请求和套接字接受失败 java.io.IOException : Too many open files

java - AWS Systems Manager 参数存储 : Using StringList as Key Value Pairs in Java (Lambda)

java - 如何在从 ITextRenderer 和 Thymeleaf HTML 模板生成的 PDF 文件中显示 Unicode(十六进制)字符

javascript - 有没有办法让 'click here to begin' 按钮位于页面底部但位于脚本标记的中心?

c - 打印 printf 中正数的前导 '+'

java - java中的打卡时钟

java - 惰性初始化异常 : Why can't Hibernate create a session on lazy loading?

ruby-on-rails - 如果我的日期格式在Rails中不正确,如何正确显示验证错误?