Converting number to word

72.6k views Asked by At

I would like to get some help on converting an integer to word. I am very new to Java, i kind of have an idea of what i think im going to be doing but need some help with it.

The code should print the words of number from 0-999 and once -1 typed in the scanner the program should stop.

Like this:

Please type a number between 0 and 999: 1

ONE

Please type a number between 0 and 999: 11

ELEVEN

Please type a number between 0 and 999: 122

ONE HUNDRED AND TWENTY TWO

Please type a number between 0 and 999: 1000

NUMBER OUT OF RANGE

Please type a number between 0 and 999: -1

Thank you for using our program

I also have to use methods to make this "Cleaner"

9

There are 9 answers

3
kriyeta On BEST ANSWER

First of all take hundreds place digit by deviding by 100 and print corresponding number by calling method numberToWord((number / 100), " HUNDRED"); since number / 100 would be in between 0 to 9 so it will print digit in word concatenated by HUNDRED.

Now you left with two digit number for that you directly call numberToWord((number % 100), " "); since we are taking modulo 100 of number so it would pass two digit only. if (num > 19) {System.out.print(tens[num / 10] + " " + ones[num % 10]);} then it will take tens place and print tens word concatenated by ones. else {System.out.print(ones[num]);} directly prints word between 1 to 19 from the array.

import java.util.Scanner;

public class Test1 {



        public static void main(String[] args) {
            int number = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
            number = scanner.nextInt();
            while(number!=-1){
                if(number>=0 && number<=999){
                    if(number==0){
                        System.out.print("NUMBER AFTER CONVERSION:\tZERO");
                    } else {
                        System.out.print("NUMBER AFTER CONVERSION:\t");
                        numberToWord(((number / 100) % 10), " HUNDRED");
                        numberToWord((number % 100), " ");
                    }

                } else{
                    System.out.print("NUMBER OUT OF RANGE");
                }
                System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit:  ");
                number = scanner.nextInt();
            }
        }

        public static void numberToWord(int num, String val) {
            String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
            };
            String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
            if (num > 19) {
                System.out.print(tens[num / 10] + " " + ones[num % 10]);
            } else {
                System.out.print(ones[num]);
            }
            if (num > 0) {
                System.out.print(val);
            }
        }
    }

Sample output:

Please type a number between 0 and 999 OR type -1 to exit:  563
NUMBER AFTER CONVERSION:     FIVE HUNDRED SIXTY  THREE 
Please type a number between 0 and 999 OR type -1 to exit:  45
NUMBER AFTER CONVERSION:      FOURTY  FIVE 
Please type a number between 0 and 999 OR type -1 to exit:  6 
NUMBER AFTER CONVERSION:      SIX 
Please type a number between 0 and 999 OR type -1 to exit:  0
NUMBER AFTER CONVERSION:    ZERO 
Please type a number between 0 and 999 OR type -1 to exit:  -1
exit
1
Rohit Jain On

This is recursive solution of Number to Word program

 string unitsMap[] = { "zero", "one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tensMap[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

string NumberToWords(int number)
{
 if (number == 0)
    return "zero";

if (number < 0)
    return "minus " + NumberToWords(abs(number));

string words = "";

if ((number / 1000000000) > 0)
{
    words += NumberToWords(number / 1000000000) + " billion ";
    number %= 1000000000;
}

if ((number / 1000000) > 0)
{
    words += NumberToWords(number / 1000000) + " million ";
    number %= 1000000;
}

if ((number / 1000) > 0)
{
    words += NumberToWords(number / 1000) + " thousand ";
    number %= 1000;
}

if ((number / 100) > 0)
{
    words += NumberToWords(number / 100) + " hundred ";
    number %= 100;
}

if (number > 0)
{
    if (number < 20)
        words += unitsMap[number];
    else
    {
        words += tensMap[number / 10];
        if ((number % 10) > 0)
            words += "-" + unitsMap[number % 10];
    }
}

return words;
 }
0
Akshay Chopra On
import java.util.Scanner;

public class number to words  {

  static String string;
  static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
  static String st2[]={"hundred","thousand","lakh","crore"};
  static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
  static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

  public static void main(String args[])
  {
    Scanner in=new Scanner(System.in);
    System.out.println("ENTER THE NUMBER");
    System.out.println(mac(in.nextInt())+" only");
  }
  public static String mac(int n)
  {
    int word;
    int nu=1;
    string="";
    while(n!=0)
    {
        switch(nu)
        {
        case 1:
            word=n%100;
            pass(word);
            if(n>100&&n%100!=0)
            {
                show("and ");
            }
            n=n/100;
            break;
        case 2:
            word=n%10;
            if(word!=0)
            {
                show(" ");
                show(st2[0]);
                show(" ");
                pass(word);
            }
            n=n/10;
            break;
        case 3:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[1]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 4:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[2]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 5:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[3]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        }
        nu++;
    }
    return string;
  }
  public static void pass(int n)
  {
    int word,q;
    if(n<10)
    {
        show(st1[n]);
    }
    if(n>9&&n<20)
    {
        show(st3[n-10]);
    }
    if(n>19)
    {
        word=n%10;
        if(word==0)
        {
            q=n/10;
            show(st4[q-2]);
        }
        else
        {
            q=n/10;
            show(st1[word]);
            show(" ");
            show(st4[q-2]);
        }
    }
  }
  public static void show(String s)
  {
    String st=string;
    string=s+st;
  }
}
0
pushpendra yadav On

//This code can print number to word from 0 to Crore

public class NumericToWords {

static Map<Long, String> map1 = new HashMap<Long, String>();

public static void main(String[] args) {
    Long num = new Long(300001);
    print(num, num.toString().length());
}

private static void print(Long num, int length) {
    if (length == 0) {
        System.out.println("Enter correct number");
    } else if (length == 1 || num <= 9) {
        printOneDigit(num);
    } else if (length == 2 || num <= 99) {
        printTwoDigit(num);
    } else if (length == 3 || num <= 999) {
        printThreeDigit(num);
    } else if (length == 4 || num <= 9999) {
        printFourDigit(num);
    } else if (length == 5 && num <= 99999) {
        printFiveDigit(num);
    } else if (length == 6 || num <= 999999) {
        printSixDigit(num);
    } else if (length == 7 || num <= 9999999) {
        printSevenDigit(num);
    } else if (length == 7 || num <= 99999999) {
        printEightDigit(num);
    } else
        System.out.println("Number is not valid....!!!!!");
}

private static void printOneDigit(Long num) {
    System.out.print(map1.get(num) != null ? map1.get(num) : "" + " ");
}

private static void printTwoDigit(Long num) {
    if (num % 10 == 0)
        System.out.print(map1.get(num) + "");
    else if (num < 20) {
        System.out.print(map1.get(num) + " ");
    } else {
        System.out.print(map1.get(num - num % 10) != null ? map1.get(num - num % 10) : "");
        System.out.print(" ");
        printOneDigit(num % 10);
    }
}

private static void printThreeDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0)
        System.out.print(map1.get(num / 100) + " HUNDRED");
    else {
        System.out.print(map1.get(num / 100) + " HUNDRED ");
        print(num % 100, 2);
    }
}

private static void printFourDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0 && num % 1000 == 0)
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
    else {
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
        print(num % 1000, 3);
    }
}

private static void printFiveDigit(Long num) {
    if (num / 1000 > 0) {
        printTwoDigit(num / 1000);
        System.out.print(" THOUSAND ");
    }
    if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printSixDigit(Long num) {
    if (num / 100000 > 0) {
        printOneDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num > 0)
        printFiveDigit(num % 100000);
}

private static void printSevenDigit(Long num) {
    if (num / 100000 > 0) {
        printTwoDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num % 100000 > 9999) {
        printFiveDigit(num % 100000);
    } else if ((num % 10000) > 999) {
        printFourDigit(num % 10000);
    } else if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printEightDigit(Long num) {
    printOneDigit(num / 10000000);
    System.out.print(" CRORE ");
    if (num > 0)
        printSevenDigit(num % 10000000);
}

static {
    map1.put(0L, "");
    map1.put(1L, "ONE");
    map1.put(2L, "TWO");
    map1.put(3L, "THREE");
    map1.put(4L, "FOUR");
    map1.put(5L, "FIVE");
    map1.put(6L, "SIX");
    map1.put(7L, "SEVEN");
    map1.put(8L, "EIGHT");
    map1.put(9L, "NINE");
    map1.put(10L, "TEN");
    map1.put(11L, "ELEVEN");
    map1.put(12L, "TWELVE");
    map1.put(13L, "THIRTEEN");
    map1.put(14L, "FOURTEEN");
    map1.put(15L, "FIFTEEN");
    map1.put(16L, "SIXTEEN");
    map1.put(17L, "SEVENTEEN");
    map1.put(18L, "EIGHTEEN");
    map1.put(19L, "NINTEEN");
    map1.put(20L, "TWINITY");
    map1.put(30L, "THIRTY");
    map1.put(40L, "FOURTY");
    map1.put(50L, "FIFTY");
    map1.put(60L, "SIXTY");
    map1.put(70L, "SEVENTY");
    map1.put(80L, "EIGHTY");
    map1.put(90L, "NINTY");
}

}

0
J.Khan On

Here is how you do it. I made it from 1 to 10 but you can expand it from 1 to 999.

   //     values entered        |     result
   //     2                     |     You have entered:Two
   //     10                    |     You have entered: Ten
   //     23                    |     You have entered: 23 which is not in range.

int i = Integer.parseInt(txtInput.getText());
    String[] namesOfNums = {"One", "Two", "Three","Four", "Five", "Six", "Seven","Eight", "Nine","Ten" };
    String output = "";
    if (i > 0 && i <=10) 
    {

        output = namesOfNums[i - 1];
        lblDisplay.setText("You have entered: "+ output);

    }
    else
    {
      lblDisplay.setText("You have entered: "+ i +" which is not range.");
    }
0
DHARMRAJSINGH On

This the solution for up to 66 digit number and can be modified according to different-different counting system. For understanding of this counting system please refer https://ptdeepali.wordpress.com/2013/10/28/vedic-numbering-system/ .

package com.number_to_word;

import java.util.Scanner;

/**
 * This program is to get the word value of given number. 
 * <p>Number can be ranges from 0 to 66th power of 10 (66 digits).</p>
 * Number can be passed through Scanner and it will print the word value of that number.
 * 
 * @author DHARAMRAJSINGH
 * 
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter number: ");
        String n = sc.nextLine();
        System.out.println(getWord(n));
        sc.close();
    }

    /**
     * This method is using array inside it and this array is 2D array contains word values.
     * <p>This array ranges from arr[0][0] to arr[9][9]. </p>
     * <p>The array is initialized in a way that it will return values like, for arr[0][0] returns zero 
     * </br> arr[0][1] returns one, arr[2][1] returns twenty one, arr[9][5] returns ninety five, and so on. </p>
     * The logic is the number <strong>n</strong> is passed into it and divided by 10, the quotient will be index0 and remainder will be index1,
     * and return arr[index0][index1].
     * </br>  
     * @param n will be in the range of 0 to 99
     * @return the word value ranges from 0 to 99
     */
    private static String getValue(int n) {
        int index0 = 0, index1 = 0;
        if(n < 0 || n > 99) {
            throw new IllegalArgumentException("number cannot be less than zero or greater than 99");
        }
        if(n < 10) {
            index1 = n;
        } else {
            index1 = n%10;
            index0 = n/10;
        }
        String arr [][] = {
                // for "zero", "one" .... "nine"
                {"शुन्य",   "एकः",  "द्वौ", "त्रयः",    "चत्वारः",  "पञ्च", "षट्",  "सप्त", "अष्ट", "नव"},
                // for "ten", "eleven" .... "nineteen"
                {"दश",  "एकादशम्",  "द्वादशम्", "त्रयोदशम्",    "चतुर्दशम्",    "पञ्चदशम्", "षोडशम्",   "सप्तदशम्", "अष्टादशम्", "नवदशम्"},
                // for "twenty", "twenty one" .... "twenty nine"
                {"विंशति",  "एकाविंशति",    "द्वाविंशति",   "त्रयोविंशति",  "चतुर्विंशति",  "पञ्चविंशति",   "षड्विंशति",    "सप्तविंशति",   "अष्टाविंशति", "नवविंशति"},
                // for "thirty", "twenty one" .... "thirty nine"
                {"त्रिंशत्",    "एकत्रिंशत्",   "द्वात्रिंशत्", "त्रयत्रिंशत्", "चतुस्त्रिंशत्",    "पञ्चत्रिंशत्", "षट्त्रिंशत्",  "सप्तत्रिंशत्", "अष्टात्रिंशत्", "एकोनचत्वारिंशत्"},
                {"चत्वारिंशत्", "एकचत्वारिंशत्",    "द्विचत्वारिंशत्",  "त्रिचत्वारिंशत्",  "चतुश्चत्वारिंशत्", "पञ्चचत्वारिंशत्",  "षट्चत्वारिंशत्", "सप्तचत्वारिंशत्",    "अष्टचत्वारिंशत्",  "एकोनपञ्चाशत्"},
                {"पञ्चाशत्",    "एकपञ्चाशत्",   "द्विपञ्चाशत्", "त्रिपञ्चाशत्", "चतुःपञ्चाशत्", "पञ्चपञ्चाशत्", "षट्पञ्चाशत्",  "सप्तपञ्चाशत्", "अष्टपञ्चाशत्","एकोनषष्ठिः"},
                {"षष्ठिः",      "एकषष्ठिः", "द्विषष्ठिः",   "त्रिषष्ठिः",   "चतुःषष्ठिः",   "पञ्चषष्ठिः",   "षट्षष्ठिः",    "सप्तषष्ठिः",   "अष्टषष्ठिः",   "एकोनसप्ततिः"},
                {"सप्ततिः", "एकसप्ततिः",    "द्विसप्ततिः",  "त्रिसप्ततिः",  "चतुःसप्ततिः",  "पञ्चसप्ततिः",  "षट्सप्ततिः",   "सप्तसप्ततिः","अष्टसप्ततिः","एकोनाशीतिः"},
                {"अशीतिः",  "एकाशीतिः", "द्वशीतिः", "त्र्यशीतिः",   "चतुरशीतिः",    "पञ्चाशीतिः",   "षडशीतिः",  "सप्ताशीतिः",   "अष्टाशीतिः","एकोननवतिः"},
                // for "ninety", "two" .... "ninety nine"
                {"नवतिः",   "एकनवतिः",  "द्विनवतिः",    "त्रिनवतिः",    "चतुर्नवतिः",   "पञ्चनवतिः",    "षण्णवतिः", "सप्तनवतिः",    "अष्टनवतिः","एकोनशतम्"}

        };
        return arr[index0][index1];
    }
    /**
     * This method returns the place value of the digit, like for index value 2 it will return hundred, 
     * for index value 3 and 4 it will thousand, for index value 5 and 6 it will lacs, and so on. 
     * <p>It return empty string for index 0 and 1 as it does not have specific name for that. 
     * @param n is index value of digit
     * @return place value of index (position like unit place, tens place etc.)
     */
    private static String getPlaceValue(int n) {
        String [] arr = {"", "", "शत", "सहस्त्र", "सहस्त्र", "लक्ष", "लक्ष", // this line is same as "", "", "hundred", "thousand", "thousand", "lac", "lac"
                "कोटि", "कोटि", "कोटि", "कोटि", "कोटि", // same as "crore" , "crore" , "crore" and so on
                "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", 
                "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु",
                "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", 
                "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद",
                "पद्म", "पद्म", "पद्म", "पद्म", "पद्म",
                "महापद्म", "महापद्म", "महापद्म", "महापद्म", "महापद्म", 
                "खर्व", "खर्व", "खर्व", "खर्व", "खर्व", 
                "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व",
                "समुद्र", "समुद्र", "समुद्र", "समुद्र", "समुद्र", 
                "ओघ", "ओघ", "ओघ", "ओघ", "ओघ", 
                "महोघ", "महोघ", "महोघ", "महोघ", "महोघ"
                };
        if(n >= arr.length) {
            throw new IllegalArgumentException("Not in range");
        } else {
            return arr[n];
        }
    }
    /**
     * This method takes index(position of digit) as the input and return the number of digits need to be taken in consideration of calculation.
     * <p> Like for index 0 only one digit will be passed to get it's word value, for index 1 two digits(00 to 99) need to be passed,
     * for index 2 inly one need to be passed and so on.
     * @param index position of digit
     * @return next index value
     */
    private static int numOfDigitsToInclude(int index) {
        int [] a = {1, 2, 1, 1, 2, 1, 2, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5};
        return a[index];
    }
    /**
     * This method returns next index value for calculations need to be performed.
     * @param index
     * @return
     */
    private static int nextIndex ( int index) {
        int [] a = {-1, -1, 1, 2, 2, 4, 4, 6, 6, 6, 6, 6, 11, 11, 11, 11, 11,
                16, 16, 16, 16, 16,
                21, 21, 21, 21, 21, 
                26, 26, 26, 26, 26, 
                31, 31, 31, 31, 31, 
                36, 36, 36, 36, 36, 
                41, 41, 41, 41, 41, 
                46, 46, 46, 46, 46, 
                51, 51, 51, 51, 51, 
                56, 56, 56, 56, 56, 
                61, 61, 61, 61, 61
        };
        return a[index];
    }

    /**
     * This method takes number as input in String(as it exceeds the limit for long) form.
     * <p> Convert into array and loop over it to get word value for number.
     * @param num
     * @return
     */
    private static String getWord(String num) {
        if(num.length() == 0) {
            throw new IllegalArgumentException("Not valid");
        }
        if(num.equals("0")){
            return getValue(0);
        }
        String s = "" + num;
        int size = s.length();
        long [] a = new long [size];
        String n = num; int k=0;

        for(int j = num.length() -1 ; j >= 0; j--) {
            a[k++] = Integer.parseInt(""+ num.charAt(j) );
        }
        int i = a.length-1;
        int arg = 0;
        int include = 0;
        StringBuffer buffer = new StringBuffer();
        while(i >= 0) {
            arg = 0;
            include = numOfDigitsToInclude(i);
            if(include == 2) {
                arg = (int) ((int) 10*a[i] + a[i-1]);
            } else if(include == 1) {
                arg = (int) a[i];
            } else {
                for(int l = 1; l<=include; l++) {
                    arg =  (int) (10*arg + a[i+1-l]);
                }
                //recursion used for arg value more than 99
                buffer.append(getWord(""+ arg)).append(" ");
            }
            if(arg == 0) {
                i = nextIndex(i);
                if(i <= 0) {
                    continue;
                }
                continue;
            }
            if(include <= 2) {
                buffer.append(getValue(arg)).append( " ");
            }
            buffer.append(getPlaceValue(i)).append(" ");
            i = nextIndex(i);
        }
        return buffer.toString();
    }
}
0
shivi2207 On

try this for a single digit:

public class WordToNumber {

static String string;
static String st1[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
static int st2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

public static int mac(String n) {
    int digit = 0;
    try {
        if(Arrays.asList(st1).contains(n.toLowerCase())){
            int a = Arrays.asList(st1).indexOf(n.toLowerCase());
            digit = st2[a];
        }
        else{
            return -1;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    return digit;
}

}

0
Rathina Moorthy Nataraj On
import java.util.Scanner;
class Name
{
    static String string;
    static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
    static String st2[]={"hundred","thousand","lakh","crore"};
    static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        System.out.println(mac(in.nextInt())+" only");
    }
    public static String mac(int n)
    {
        int word;
        int nu=1;
        string="";
        while(n!=0)
        {
            switch(nu)
            {
            case 1:
                word=n%100;
                pass(word);
                if(n>100&&n%100!=0)
                {
                    show("and ");
                }
                n=n/100;
                break;
            case 2:
                word=n%10;
                if(word!=0)
                {
                    show(" ");
                    show(st2[0]);
                    show(" ");
                    pass(word);
                }
                n=n/10;
                break;
            case 3:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[1]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 4:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[2]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 5:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[3]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            }
            nu++;
        }
        return string;
    }
    public static void pass(int n)
    {
        int word,q;
        if(n<10)
        {
            show(st1[n]);
        }
        if(n>9&&n<20)
        {
            show(st3[n-10]);
        }
        if(n>19)
        {
            word=n%10;
            if(word==0)
            {
                q=n/10;
                show(st4[q-2]);
            }
            else
            {
                q=n/10;
                show(st1[word]);
                show(" ");
                show(st4[q-2]);
            }
        }
    }
    public static void show(String s)
    {
        String st=string;
        string=s+st;
    }
}
1
Kona Suresh On
public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    };

    private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
    };

    private String convertLessThanOneThousand(int number) {
        String current;

        if (number % 100 < 20){
            current = numNames[number % 100];
            number /= 100;
        }
        else {
            current = numNames[number % 10];
            number /= 10;

            current = tensNames[number % 10] + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred" + current;
    }

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

    public static void main(String[] args) {
        NumberToWord obj = new NumberToWord();
        System.out.println("*** " + obj.convert(123456789));
        System.out.println("*** " + obj.convert(-55));
    }
}

Source:http://javahungry.blogspot.com/2014/05/convert-math-number-to-equivalent-readable-word-in-java-code-with-example.html