Android development

Android converting inputted numbers to words

Android converting inputted numbers to words

Hi, developer in this Android tutorial I am discussing. How to convert inputted numbers to words in android and show it on text view. For example like you can see most app when the user enters the Amount in a number Automatically converts the numbers to words and show it.

Write code that converts the input amount into words. For instance, when “1234” is entered as an input result will be “one million two hundred three”.

Here is the implementation for the same. The code is able to handle numbers from 1 to 4 digits i.e. numbers that range between 0 and 9999. The idea is to make arrays that can store the individual components from the output string. One array is designed to store single digits. Another is used for numbers between 10 and 19, another for numbers from 20 30 40, 50 .. etc., and another that is for the power of 10. The number given is split into 2 parts, the initial two digits, and the final two digits. Both parts are printed on separate sheets numbers to words.

Android converting inputted numbers to words

Let’s start to make an example for converting input numbers to words.  Start your android studio and create an android project if you have an existing your can open your project and follow the below steps.

Steps1 :-  Create a java class with the name NumberToWord.

public class NumberToWord {

    public static final String[] units = {"", "One", "Two", "Three", "Four",
            "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
            "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
            "Eighteen", "Nineteen"};

    public static final String[] tens = {
            "",        // 0
            "",        // 1
            "Twenty",    // 2
            "Thirty",    // 3
            "Forty",    // 4
            "Fifty",    // 5
            "Sixty",    // 6
            "Seventy",    // 7
            "Eighty",    // 8
            "Ninety"    // 9
    };

    public static String convert(final int n) {
        if (n < 0) {
            return "Minus " + convert(-n);
        }

        if (n < 20) {
            return units[n];
        }

        if (n < 100) {
            return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
        }

        if (n < 1000) {
            return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
        }

        if (n < 100000) {
            return convert(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + convert(n % 1000);
        }

        if (n < 10000000) {
            return convert(n / 100000) + " Lakh" + ((n % 100000 != 0) ? " " : "") + convert(n % 100000);
        }

        return convert(n / 10000000) + " Crore" + ((n % 10000000 != 0) ? " " : "") + convert(n % 10000000);
    }


}

Steps 2:- Convert numbers to Word In your main Class.

you can use the above class for converting numbers into words. the user below method directly to convert it. And You can add TextWather in your Input filead  to track the change in edit text.

loanAmountEditText = findViewById(R.id.loanAmountEditText);
loanAmountInWord = findViewById(R.id.loanAmountInWord);
loanAmountEditText.addTextChangedListener(new TextWatcher() {
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {

   }

   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
      String text = loanAmountEditText.getText().toString().trim();
      if (text.startsWith("0")){
         loanAmountEditText.setText(text.substring(1));
      }

   }

   @Override
   public void afterTextChanged(Editable s) {
      String text = loanAmountEditText.getText().toString().trim();
      if(!text.isEmpty()){
         String AmountWord = numToWords(Integer.parseInt(text));
         loanAmountInWord.setText(AmountWord);
      }else {
         loanAmountInWord.setText("");
      }

   }
});
private String numToWords (int n){ //optional
   NumberToWord ntw = new NumberToWord(); // directly implement this
   return ntw.convert(n);
}

 

In your XML file, you can add an Edittext and TextView Edittext to input the Amount and TextView to show the converted word amount and show it.

Read More Tutorial