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.

  • How to create a Custom Dialog box in android?
  • stream volume in SoundPool vs volume in AudioManager
  • How to sync SQLite database on Android phone with MySQL database on server?
  • How to set mobile system time and date in android?
  • android:entries in recyclerview
  • Capture button release in Android
  • Please guide me.

  • Android screen size HDPI, LDPI, MDPI
  • wrap_content is not working in button height android
  • Restore purchases in In-app Billing (IAB Version 3) android
  • Android: Disable highlighting in GridView
  • How to take a screenshot and share it programmatically
  • Unit tests NPE, when I add fragment custom transitions
  • 5 Solutions collect form web for “How can I format a String number to have commas in android Edit Field”

    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

    try this one hope it will help.

     System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1000));
    
    private String getFormatedAmount(int amount){
      return NumberFormat.getNumberInstance(Locale.US).format(amount);
    }
    

    Add a text change listener as below (Also make sure that the input type selected for Edittext is Number) :

    etTest.addTextChangedListener(new TextWatcher() {
    
            boolean isManualChange = false;
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                if (isManualChange) {
                    isManualChange = false;
                    return;
                }
    
                try {
                    String value = s.toString().replace(",", "");
                    String reverseValue = new StringBuilder(value).reverse()
                            .toString();
                    StringBuilder finalValue = new StringBuilder();
                    for (int i = 1; i <= reverseValue.length(); i++) {
                        char val = reverseValue.charAt(i - 1);
                        finalValue.append(val);
                        if (i % 3 == 0 && i != reverseValue.length() && i > 0) {
                            finalValue.append(",");
                        }
                    }
                    isManualChange = true;
                    etTest.setText(finalValue.reverse());
                    etTest.setSelection(finalValue.length());
                } catch (Exception e) {
                    // Do nothing since not a number
                }
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
    
            }
        });
    
    int[] numbersToFormat = new int[]
      { 1, 10, 100, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
    
    
    for (int number : numbersToFormat) {
      System.out.println(  
        NumberFormat.getNumberInstance(Locale.getDefault()).format(number));
    }
    

    OUTPUT

    1
    10
    100
    10,000
    100,000
    1,000,000
    10,000,000
    100,000,000
    1,000,000,000
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.