[java] Converting Integer to String with comma for thousands

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?

This question is related to java string integer

The answer is


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"


 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

Output: 35,634,646


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>

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

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

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.


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


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.


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

This is by far the easiest way.


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

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

val myNumber = NumberFormat.getNumberInstance(Locale.US)
   .format(123456789)
   .replace(",", "?")

This solution worked for me:

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

int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to integer

Python: create dictionary using dict() with integer keys? How to convert datetime to integer in python Can someone explain how to append an element to an array in C programming? How to get the Power of some Integer in Swift language? python "TypeError: 'numpy.float64' object cannot be interpreted as an integer" What's the difference between integer class and numeric class in R PostgreSQL: ERROR: operator does not exist: integer = character varying C++ - how to find the length of an integer Converting binary to decimal integer output Convert floats to ints in Pandas?