Use NumberFormat
:
long num = Long.valueOf("1234567890");
String numWithCommas = java.text.NumberFormat.getInstance().format(num);
Edit:
The above code snippet will default to using the user's current locale (which I'm guessing is probably what you'd want—you want the number to be formatted in a way the user is used to seeing). If you want to set a specific locale you can pass an instance of Locale
to getInstance()
. There are a bunch of static instances available in the Locale
class for your convenience:
// Always prints with comma as separator
java.text.NumberFormat.getInstance(Locale.ENGLISH).format(num);
// => 1,234,567,890
// Always prints with period as separator
java.text.NumberFormat.getInstance(Locale.GERMAN).format(num);
// => 1.234.567.890