有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java DecimalFormat保留所有十进制数

我使用DecimalFormat将double格式化为字符串。 然后将该字符串集成到我的表示层中

  • 问题:我想保留所有小数示例:“12345678.123456789”
  • 问题:我应该使用什么格式
  • 备注:我使用不同的区域设置来支持多个布局

格式:#->;这将在小数点之前使用所有数字,但在小数点之后对数字进行四舍五入

我可以用对于大小数点,但是如果小数点更长呢

我发现我的小测试程序很有用,想和大家分享

你能帮我显示所有的小数吗

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);
    }
}

这导致:

====== 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
=============================================================

编辑:一位用户提到了一个相关问题:How to nicely format floating numbers to String without unnecessary decimal 0? 这个问题并不能解决我的问题,因为它的重点是限制大小,但我需要尽可能长的保留它


共 (2) 个答案

  1. # 1 楼答案

    绳子。这里的格式可能会帮助你-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);
      }
    

    代码输出:

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

    您会注意到%s(字符串)与语言环境不符,但会将双精度格式设置为最多17个小数点。 用绳子。格式您可以进一步优化数字在字符串中的格式