如何在Java中设置千位分隔符?

时间:2022-10-31 22:47:20

How to set thousands separator in Java? I have String representation of BigDecimal, i want to set thousands separator and return String.

如何在Java中设置千位分隔符?我有BigDecimal的String表示,我想设置千位分隔符并返回String。

8 个解决方案

#1


85  

This should work (untested, based on JavaDoc):

这应该工作(未经测试,基于JavaDoc):

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

According to the JavaDoc, the cast in the first line should be save for most locales.

根据JavaDoc,第一行中的强制转换应该保存为大多数语言环境。

#2


177  

You can use format function with ",";

您可以使用格式功能“,”;

int no = 124750;
String str = String.format("%,d", no);

//str = 124,750

"," includes locale-specific grouping characters.

“,”包括特定于语言环境的分组字符。

docs

文档

#3


42  

BigDecimal bd = new BigDecimal(300000);

NumberFormat formatter = NumberFormat.getInstance(new Locale("en_US"));

System.out.println(formatter.format(bd.longValue()));

EDIT

编辑

To get custom grouping separator such as space, do this:

要获取自定义分组分隔符(如空格),请执行以下操作:

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setGroupingSeparator(' ');

DecimalFormat formatter = new DecimalFormat("###,###.##", symbols);
System.out.println(formatter.format(bd.longValue()));

#4


11  

try this code to format as used in Brazil:

尝试使用此代码格式化在巴西使用:

    DecimalFormat df = new DecimalFormat(
      "#,##0.00", 
      new DecimalFormatSymbols(new Locale("pt", "BR")));

    BigDecimal value = new BigDecimal(123456.00);

    System.out.println(df.format(value.floatValue()));

    // results: "123.456,00"

#5


3  

DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setDecimalSeparator('|');
formatSymbols.setGroupingSeparator(' ');

String strange = "#,##0.###";
DecimalFormat df = new DecimalFormat(strange, formatSymbols);
df.setGroupingSize(4);

String out = df.format(new BigDecimal(300000).doubleValue());

System.out.println(out);

#6


3  

The accepted answer has to be really altered otherwise not working. The getDecimalFormatSymbols makes a defensive copy. Thus,

接受的答案必须真正改变,否则不起作用。 getDecimalFormatSymbols制作防御性副本。从而,

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

The new line is this one: formatter.setDecimalFormatSymbols(symbols);

新行是这一行:formatter.setDecimalFormatSymbols(symbols);

#7


2  

NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);

#8


0  

For decimals:

对于小数:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
DecimalFormat dfDecimal = new DecimalFormat("###########0.00###");
dfDecimal.setDecimalFormatSymbols(symbols);
dfDecimal.setGroupingSize(3);
dfDecimal.setGroupingUsed(true);
System.out.println(dfDecimal.format(number));

#1


85  

This should work (untested, based on JavaDoc):

这应该工作(未经测试,基于JavaDoc):

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

According to the JavaDoc, the cast in the first line should be save for most locales.

根据JavaDoc,第一行中的强制转换应该保存为大多数语言环境。

#2


177  

You can use format function with ",";

您可以使用格式功能“,”;

int no = 124750;
String str = String.format("%,d", no);

//str = 124,750

"," includes locale-specific grouping characters.

“,”包括特定于语言环境的分组字符。

docs

文档

#3


42  

BigDecimal bd = new BigDecimal(300000);

NumberFormat formatter = NumberFormat.getInstance(new Locale("en_US"));

System.out.println(formatter.format(bd.longValue()));

EDIT

编辑

To get custom grouping separator such as space, do this:

要获取自定义分组分隔符(如空格),请执行以下操作:

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setGroupingSeparator(' ');

DecimalFormat formatter = new DecimalFormat("###,###.##", symbols);
System.out.println(formatter.format(bd.longValue()));

#4


11  

try this code to format as used in Brazil:

尝试使用此代码格式化在巴西使用:

    DecimalFormat df = new DecimalFormat(
      "#,##0.00", 
      new DecimalFormatSymbols(new Locale("pt", "BR")));

    BigDecimal value = new BigDecimal(123456.00);

    System.out.println(df.format(value.floatValue()));

    // results: "123.456,00"

#5


3  

DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setDecimalSeparator('|');
formatSymbols.setGroupingSeparator(' ');

String strange = "#,##0.###";
DecimalFormat df = new DecimalFormat(strange, formatSymbols);
df.setGroupingSize(4);

String out = df.format(new BigDecimal(300000).doubleValue());

System.out.println(out);

#6


3  

The accepted answer has to be really altered otherwise not working. The getDecimalFormatSymbols makes a defensive copy. Thus,

接受的答案必须真正改变,否则不起作用。 getDecimalFormatSymbols制作防御性副本。从而,

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

The new line is this one: formatter.setDecimalFormatSymbols(symbols);

新行是这一行:formatter.setDecimalFormatSymbols(symbols);

#7


2  

NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);

#8


0  

For decimals:

对于小数:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
DecimalFormat dfDecimal = new DecimalFormat("###########0.00###");
dfDecimal.setDecimalFormatSymbols(symbols);
dfDecimal.setGroupingSize(3);
dfDecimal.setGroupingUsed(true);
System.out.println(dfDecimal.format(number));