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?

FThompson
  • 28,352
  • 13
  • 60
  • 93
Dominik
  • 4,718
  • 13
  • 44
  • 58

15 Answers15

324
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
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"

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
98
int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);
mopsled
  • 8,445
  • 1
  • 38
  • 40
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.

Frankie
  • 24,627
  • 10
  • 79
  • 121
21
 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

Output: 35,634,646

Miha_x64
  • 5,973
  • 1
  • 41
  • 63
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 1
    Your pattern need not be more than `#,###`, if you want european style formatting then you can use `#,##,###`. – Ali Aug 15 '11 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 02 '13 at 07:34
  • I get it... fine, if you need indian formatting then that's how you do it. – Ali Jun 14 '13 at 03: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 '16 at 01: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
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
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
Hun
  • 3,652
  • 35
  • 72
4

This solution worked for me:

NumberFormat.getNumberInstance(Locale.US).format(Integer.valueOf("String Your Number"));
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
A S
  • 71
  • 6
4

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

This is by far the easiest way.

perror
  • 7,071
  • 16
  • 58
  • 85
Annie Johnson
  • 41
  • 1
  • 3
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
  • Consider to [edit](https://stackoverflow.com/posts/55066145/edit) your answer adding some comments to your code (and maybe wrap it in a method, so it can be more useful) – noiaverbale Mar 08 '19 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>
Navankur Chauhan
  • 407
  • 6
  • 22
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(",", "،")
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
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.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Josh
  • 1
-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

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Ankur
  • 1