简体   繁体   English

DecimalFormat - 保留所有十进制数

[英]DecimalFormat - keep all decimal numbers

I'm using DecimalFormat to format doubles into a String. 我正在使用DecimalFormat将双精度格式化为String。 This String is then integrated into my presentation layer. 然后将此String集成到我的表示层中。

  • Problem : I want to keep ALL decimals. 问题 :我想保留所有小数。 Example: "12345678.123456789" 示例:“12345678.123456789”
  • Question : What format should I use? 问题 :我应该使用什么格式?
  • Remark : I use a different Locale to support multiple layouts. 备注 :我使用不同的Locale来支持多种布局。

Format: #.## -> This uses ALL numbers BEFORE the decimal, but ROUNDS the numbers AFTER the decimal. 格式: #。## - >这使用小数点所有数字,但是将小数点的数字换成ROUNDS

I could use #.######### for the big decimal, but what if the decimal is even longer? 我可以使用#。#########作为大小数,但是如果小数更长的话怎么办?

I found my small test program useful and want to share it with you. 我发现我的小测试程序很有用,并希望与您分享。

Can you help me to show ALL decimals? 你能帮我显示所有小数吗?

package be.softwarelab.numbers;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class DecimalNumbersTest {

    private static final double NUMBER = 123456789.123456789;

    public static void main(String[] args) {

        String format01 = "#.";
        String format02 = "#.#";
        String format03 = "#.##";
        String format04 = "#.###";
        String format05 = "#.####"; 

        System.out.println("====== NUMBER ===== USA =====================================");
        showResult(NUMBER, format01, Locale.US);
        showResult(NUMBER, format02, Locale.US);
        showResult(NUMBER, format03, Locale.US);
        showResult(NUMBER, format04, Locale.US);
        showResult(NUMBER, format05, Locale.US);
        System.out.println("====== NUMBER ===== France ==================================");
        showResult(NUMBER, format01, Locale.FRANCE);
        showResult(NUMBER, format02, Locale.FRANCE);
        showResult(NUMBER, format03, Locale.FRANCE);
        showResult(NUMBER, format04, Locale.FRANCE);
        showResult(NUMBER, format05, Locale.FRANCE);
        System.out.println("=============================================================");
    }

    public static void showResult(double number, String format, Locale locale) {
        // Using a Locale to see the differences between regions.
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(locale);
        DecimalFormat formatter = new DecimalFormat (format, otherSymbols);

        // Create the String result
        String output = formatter.format(number);

        // Format the output for a nice presentation.
        System.out.format("    %s %11s = %20s\n", locale, format, output);
    }
}

This results in: 这导致:

====== NUMBER ===== USA =====================================
en_US          #. =           123456789.
en_US         #.# =          123456789.1
en_US        #.## =         123456789.12
en_US       #.### =        123456789.123
en_US      #.#### =       123456789.1235
====== NUMBER ===== France ==================================
fr_FR          #. =           123456789,
fr_FR         #.# =          123456789,1
fr_FR        #.## =         123456789,12
fr_FR       #.### =        123456789,123
fr_FR      #.#### =       123456789,1235
=============================================================

Edit: One user mentioned a related question: How to nicely format floating numbers to String without unnecessary decimal 0? 编辑:一位用户提到了一个相关的问题: 如何很好地将浮动数字格式化为字符串而没有不必要的小数0? This question does not solve my problems, since it focuses on limiting the size, but I need to keep it as long as possible. 这个问题并没有解决我的问题,因为它侧重于限制尺寸,但我需要尽可能长时间地保持它。

String.format might help you out here - NumberFormat String.format可能会帮到你 - NumberFormat

  private static void printfWithLocale(Locale locale, Double d){
    System.out.println("Output locale: " + locale.toString());
    String simpleOutputTeplate = "simpleOutputTeplate: %s";
    String refinedOutputTeplate = "refinedOutputTeplate: %.10f";

    System.out.println(String.format(locale, simpleOutputTeplate, d));
    System.out.println(String.format(locale, refinedOutputTeplate, d));

  }

  public static void main(String[] args) {

    Double d = new Double(3.1234567890123456789);

    printfWithLocale(Locale.US, d);
    System.out.println("");
    printfWithLocale(Locale.FRANCE, d);
  }

Code output: 代码输出:

Output locale: en_US
simpleOutputTeplate: 3.1234567890123457
refinedOutputTeplate: 3.1234567890

Output locale: fr_FR
simpleOutputTeplate: 3.1234567890123457
refinedOutputTeplate: 3,1234567890

You will notice that the %s (string) does not conform to the Locale, but does format the Double up to 17 decimal points. 您会注意到%s(字符串)不符合Locale,但会将Double格式化为最多17个小数点。 With String.format you can further refine the way your numbers are formatted in a string. 使用String.format,您可以进一步细化数字在字符串中的格式。

Since double does not exactly represent some numbers, it doesn't matter if you cut to a given length of decimals (like 17). 由于double并不完全代表某些数字,因此如果切割到给定的小数长度(如17)则无关紧要。 See this other question for the explanation How many significant digits have floats and doubles in java? 请参阅这个解释的另一个问题有多少有效数字在java中有浮点数和双精度数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM