13

我正在尝试格式化字符串以在 3 位组之间添加逗号

例如:

1200.20 >> 1,200.20
15000   >> 15,000

我正在尝试弄清楚如何使用 DecimalFormat 来完成它,到目前为止,我一直在使用我自己的一个似乎过于复杂的脚本。我不知道该怎么做,使用 # 只是隐藏尾随零并使用 0 将它们添加到数字中。

这就是我现在正在尝试的:

DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
resultStr = df.format(Double.valueOf(resultStr));

我敢肯定这一定很容易,但我不知道该怎么做。我不必用 DecimalFormat 来做,我只是认为这会是更简单的方法。我怎样才能简单地添加逗号而不以任何方式修改小数?

4

5 回答 5

21

您应该使用 NumberFormat 对象并将其设置为使用分组。就像是

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

public class NumberFormatEg {
   public static void main(String[] args) {
      NumberFormat myFormat = NumberFormat.getInstance();
      myFormat.setGroupingUsed(true);

      double[] numbers = { 11220.00, 232323232.24, 121211.55, 102.121212 };

      for (double d : numbers) {
         System.out.println(myFormat.format(d));
      }
      System.out.println();

      DecimalFormat decimalFormat = new DecimalFormat("#.00");
      decimalFormat.setGroupingUsed(true);
      decimalFormat.setGroupingSize(3);

      for (double d : numbers) {
         System.out.println(decimalFormat.format(d));
      }

      System.out.println("\nFor Germany");

      NumberFormat anotherFormat = NumberFormat
            .getNumberInstance(Locale.GERMAN);
      if (anotherFormat instanceof DecimalFormat) {
         DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
         anotherDFormat.applyPattern("#.00");
         anotherDFormat.setGroupingUsed(true);
         anotherDFormat.setGroupingSize(3);

         for (double d : numbers) {
            System.out.println(anotherDFormat.format(d));
         }

      }

      System.out.println("\nFor US:");

      anotherFormat = NumberFormat.getNumberInstance(Locale.US);
      if (anotherFormat instanceof DecimalFormat) {
         DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
         anotherDFormat.applyPattern("#.00");
         anotherDFormat.setGroupingUsed(true);
         anotherDFormat.setGroupingSize(3);

         for (double d : numbers) {
            System.out.println(anotherDFormat.format(d));
         }

      }
   }
}

返回:

11,220
232,323,232.24
121,211.55
102.121

11,220.00
232,323,232.24
121,211.55
102.12

For Germany
11.220,00
232.323.232,24
121.211,55
102,12

For US:
11,220.00
232,323,232.24
121,211.55
102.12

这样做的一个优点是解决方案可以是特定于语言环境的。

Edited
Now 显示了一个带有 DecimalFormat 对象的示例。请注意,如果您使用它,您应该设置分组大小。

于 2013-03-10T22:46:00.337 回答
14

您也可以尝试类似

DecimalFormat df = new DecimalFormat("#,###.00");

System.out.println(df.format(1200.20));
System.out.println(df.format(15000));
System.out.println(df.format(123456789.99));
1,200.20
15,000.00
123,456,789.99
于 2013-03-10T22:55:02.583 回答
2

你应该能够做你想做的事:

http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

DecimalFormat myFormatter = new DecimalFormat("$###,###.###");
String output = myFormatter.format(12345.67);
System.out.println(value + " " + pattern + " " + output);
于 2013-03-10T22:49:43.050 回答
2

最简单的解决方案

为什么不将逗号,标志printf.

System.out.printf( "%,d\n", 58625 );// the d to accept decimal integer
System.out.printf( "%,.2f", 12345678.9 );// the f to accept folouting point and 2 to take only 2 digits  

输出将是

58,625
12,345,678.90

好消息:使用的实际生成的分隔符is specific to the user’s locale

于 2017-02-08T17:47:29.543 回答
1

我无法使任何其他解决方案起作用,一些四舍五入,一些添加尾随零或一些删除尾随零。

我不打算接受我自己的答案,但我会发布我的脚本以防有人发现它有用。

public void(String str) {
    int floatPos = str.indexOf(".") > -1 ? str.length() - str.indexOf(".") : 0;
    int nGroups= (str.length()-floatPos-1-(str.indexOf("-")>-1?1:0))/3;
    for(int i=0; i<nGroups; i++){
        int commaPos = str.length() - i * 4 - 3 - floatPos;
    str = str.substring(0,commaPos) + "," + str.substring(commaPos,str.length());
    }
    return str;
}


1             => 1
1.0           => 1.0
1234.01       => 1,234.01
1100100.12345 => 1,100,100.12345
于 2013-03-11T00:02:06.873 回答