Converting Integer to String with comma for thousands
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
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 bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);
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.