212

I want to convert an Integer 35634646 to have the thousand "," so it should be 35,634,646.

What would be the quickest way to doing that?

0

15 Answers 15

324
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
6
  • 6
    I had to add import java.text.NumberFormat;.
    – Mike S
    May 11, 2015 at 21:09
  • 6
    This forces the US style grouping which always uses ,, however there are also regions using . for grouping instead. In a word, hard-coding a locale is considered a bad practice.
    – Hai Zhang
    Sep 26, 2017 at 21:42
  • 4
    @DreaminginCode To do it well Locale.getCurrent() is the solution
    – Roger RV
    Dec 27, 2017 at 23:54
  • @RogerRV There is no Locale.getCurrent() (at least on android)
    – E.Akio
    Mar 13, 2019 at 14:19
  • Yeh this is not the best answer for the 96% of people who don't live in the USA
    – fig
    Jun 27, 2019 at 9:33
166

You ask for quickest, but perhaps you mean "best" or "correct" or "typical"?

You also ask for commas to indicate thousands, but perhaps you mean "in normal human readable form according to the local custom of your user"?

You do it as so:

    int i = 35634646;
    String s = NumberFormat.getIntegerInstance().format(i);

Americans will get "35,634,646"

Germans will get "35.634.646"

Swiss Germans will get "35'634'646"

1
  • Where can I get the number pattern of each locale? I tried to read the code but can't find it anywhere
    – nhoxbypass
    Oct 31, 2018 at 3:01
98
int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);
2
57

Integers:

int value = 100000; 
String.format("%,d", value); // outputs 100,000

Doubles:

double value = 21403.3144d;
String.format("%,.2f", value); // outputs 21,403.31

String.format is pretty powerful.

- Edited per psuzzi feedback.

4
  • 1
    Advantage: No "new", does not load up the garbage collector (unlike DecimalFormat).
    – Contango
    Sep 15, 2017 at 10:27
  • Have a look in String.format. It starts with return new Formatter(l) ... etc. So the garbage collector argument is not as strong at it seems.
    – Scheintod
    Sep 25, 2017 at 19:59
  • 3
    This is my favorite answer, the question asks to work with Integer, so here is the code: int value = 100000; String.format("%,d", value);
    – psuzzi
    Mar 6, 2019 at 22:41
  • 1
    I like this one.
    – Sam Chen
    Sep 25, 2020 at 16:23
21
 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

Output: 35,634,646

4
  • 1
    Your pattern need not be more than #,###, if you want european style formatting then you can use #,##,###.
    – Ali
    Aug 15, 2011 at 20:25
  • 8
    @Ali: The format you describe is only used in India, and I'm at least 90% sure India isn't in Europe.
    – Cairnarvon
    Jun 2, 2013 at 7:34
  • I get it... fine, if you need indian formatting then that's how you do it.
    – Ali
    Jun 14, 2013 at 3:42
  • 1
    This is locale dependent and will not produce the correct (as per question) output if run where the default locale doesn't separate thousands with a ,.
    – antak
    Apr 22, 2016 at 1:38
18

The other answers are correct, however double-check your locale before using "%,d":

Locale.setDefault(Locale.US);
int bigNumber = 35634646;
String formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Locale.setDefault(new Locale("pl", "PL"));
formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Result:

35,634,646
35 634 646
6

use Extension

import java.text.NumberFormat

val Int.commaString: String
  get() = NumberFormat.getInstance().format(this)

val String.commaString: String
  get() = NumberFormat.getNumberInstance().format(this.toDouble())

val Long.commaString: String
  get() = NumberFormat.getInstance().format(this)

val Double.commaString: String
  get() = NumberFormat.getInstance().format(this)

result

1234.commaString => 1,234
"1234.456".commaString => 1,234.456
1234567890123456789.commaString => 1,234,567,890,123,456,789
1234.456.commaString => 1,234.456
4

This solution worked for me:

NumberFormat.getNumberInstance(Locale.US).format(Integer.valueOf("String Your Number"));
4

Use the %d format specifier with a comma: %,d

This is by far the easiest way.

1

here's a solution for those of you who can't access "numberformat" nor "String.format" (using a limited version of java inside a framework). Hope it's useful.

number= 123456789;
thousandsSeparator=",";
myNumberString=number.toString();   
numberLength=myNumberString.length;
howManySeparators=Math.floor((numberLength-1)/3)
formattedString=myNumberString.substring(0,numberLength-(howManySeparators*3))
while (howManySeparators>0)    {
formattedString=formattedString+thousandsSeparator+myNumberString.substring(numberLength-(howManySeparators*3),numberLength-((howManySeparators-1)*3));
howManySeparators=howManySeparators-1;    }

formattedString
1
  • Consider to edit your answer adding some comments to your code (and maybe wrap it in a method, so it can be more useful) Mar 8, 2019 at 15:38
0

If the same has to be done in the JSP , use:

<fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
<c:out value="${formattedVariable}"></c:out>

ofcourse for multiple values use :

<c:forEach items="${yourlist}" var="yourlist">

    <fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
    <c:out value="${formattedVariable}"></c:out>
</c:forEach>
0

This is a way that also able you to replace default separator with any characters:

val myNumber = NumberFormat.getNumberInstance(Locale.US)
   .format(123456789)
   .replace(",", "،")
0
/**
@inpute 100000
@return 1,000,000
*/
    public static String addComma(long amount) {
        return NumberFormat.getNumberInstance(Locale.US).format(amount);
    }
-1

can't you use a

System.out.printf("%n%,d",int name);

The comma in the printf should add the commas into the %d inter.

Not positive about it, but works for me.

2
  • that's not compiling for me
    – Mishax
    Sep 5, 2017 at 6:32
  • "Not positive about it." Pointless. May 25, 2018 at 10:51
-8

First you need to include the JSTL tags :-

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

at the start of the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.