user1082964 user1082964 - 1 month ago 14
Android Question

How can I format a String number to have commas in android Edit Field

For what function I can use in android to display the number into different formats.

For eg:
If I enter 1000 then it should display like this 1,000.
If I enter 10000 then it should display like this 10,000.
If I enter 100000 then it should display like this 1,000,000.

Please guide me.

Answer

You should use DecimalFormat and just format the number

DecimalFormat formatter = new DecimalFormat("#,###,###");
String yourFormattedString = formatter.format(100000);

The result will be

  • 1,000,000 for 1000000
  • 10,000 for 10000
  • 1,000 for 1000

ect