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);
Source: Stackoverflow.com