Funtime
Funtime

Reputation: 2624

How to set thousands separator in Java?

How to set thousands separator in Java?
I have String representation of a BigDecimal that I want to format with a thousands separator and return as String.

Upvotes: 188

Views: 246452

Answers (12)

Kadir Erturk
Kadir Erturk

Reputation: 601

public String formatStr(float val) {
  return String.format(Locale.CANADA, "%,.2f", val);
}

formatStr(2524.2) // 2,254.20

Upvotes: 5

Waqas Ahmed
Waqas Ahmed

Reputation: 5139

If you are using thousand separator for Integer data type use 1.

1. For integer data Type

String.format("%,d\n", 58625)

Output:

58,625

2. For Floating Point data Type

String.format("%,.2f",58625.21)

Output:

58,625.21

Upvotes: 32

Mahozad
Mahozad

Reputation: 24582

Use underscore (_) to make literal numeric values in the code itself more readable:

int example = 12_004_953; // Twelve million four thousand nine hundred fifty-three

Upvotes: 1

0xjacobb
0xjacobb

Reputation: 71

As mentioned above, the following link gives you the specific country code to allow Java to localize the number. Every country has its own style.

In the link above you will find the country code which should be placed in here:

...(new Locale(<COUNTRY CODE HERE>));

Switzerland for example formats the numbers as follows:

1000.00 --> 1'000.00

country code

To achieve this, following codes works for me:

NumberFormat nf = NumberFormat.getNumberInstance(new Locale("de","CH"));
nf.setMaximumFractionDigits(2);
DecimalFormat df = (DecimalFormat)nf;
System.out.println(df.format(1000.00));

Result is as expected:

1'000.00

Upvotes: 7

akelec
akelec

Reputation: 4003

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

Upvotes: 4

Andreas Dolk
Andreas Dolk

Reputation: 114797

This should work (untested, based on 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.

Upvotes: 139

Gab
Gab

Reputation: 8333

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

Upvotes: 4

Amio.io
Amio.io

Reputation: 21585

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

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

Upvotes: 4

deldev
deldev

Reputation: 1386

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"

Upvotes: 25

syloc
syloc

Reputation: 4709

You can use format function with ",";

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

//str = 124,750

"," includes locale-specific grouping characters.

docs

Upvotes: 307

adarshr
adarshr

Reputation: 62603

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

Upvotes: 65

Dead Programmer
Dead Programmer

Reputation: 12585

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

Upvotes: 7

Related Questions