Convert Numbers To Text

Java Snippet

Convert numbers to text

This routine will convert a string representation of a number to it's text equivalent.

Try it here, although this is written in vbscript, the routine is the same.
As written, it will convert up to 999999999999999 (Trillions).

e.g. 123 converts to "One Hundred and Twenty Three"

    

Click to see versions in    vb    vbscript

This line determines the size
    final String divisor[] = {"Trillion ","Billion ","Million ", "Thousand ", ""};

You only have to add to the beginning "Quadrillion " to add another dimension, or remove "Trillion " to reduce a dimension. You could then also remove "Billion " if you did not want billions etc. Note: any addition to the array must be at the beginning, and the additional word must also include a trailing " " (space).

Java code

import javax.swing.JOptionPane;

class NumbersToText
{
    public static void main(String args[])
   {
        try
        {
            String input = JOptionPane.showInputDialog("Enter a number e.g. 12345");
            JOptionPane.showMessageDialog(null,convertToText(input));
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"invalid data error - program terminating");
        }
        System.exit(0);
    }

    public static String convertToText(String anyNumber)
    {
        String text = "";
        String minus = "";
        String validChars = "0123456789";
        String bits[] = new String[100];
        final String and[] = {"", "and "};
        final String hundred[] = {"", "Hundred "};
        final String divisor[] = {"Trillion ","Billion ","Million ", "Thousand ", ""};
        int groups[] = new int[divisor.length];
        int firstDigit=0, secondDigit=0, thirdDigit=0, H=0, A= 0,D=0,i=0;
        int flag = 0;
        int x;

        if(anyNumber.equals(""))
        {
            return "zero, or no data";
        }

        if(anyNumber.substring(0,1).equals("-"))
        {
            minus = "(minus) ";
            anyNumber = anyNumber.substring(1,anyNumber.length());
        }

        while(anyNumber.substring(0,1).equals("0"))
        {
            anyNumber = anyNumber.substring(1,anyNumber.length());
        }

        for(x=0; x<anyNumber.length(); x++)
        {
            if(validChars.indexOf(anyNumber.substring(x,x+1)) < 0)
            {
                return "error - invalid characters detected";
            }
        }

        if(anyNumber.length() > divisor.length * 3)
        {
             return "error - outside of number range";
        }

        bits[0]= ""; bits[1]= "One ";
        bits[2]= "Two "; bits[3]= "Three ";
        bits[4]= "Four "; bits[5]= "Five ";
        bits[6]= "Six "; bits[7]= "Seven ";
        bits[8]= "Eight "; bits[9]= "Nine ";
        bits[10]= "Ten "; bits[11]= "Eleven ";
        bits[12]= "Twelve "; bits[13]= "Thirteen ";
        bits[14]= "Fourteen "; bits[15]= "Fifteen ";
        bits[16]= "Sixteen "; bits[17]= "Seventeen ";
        bits[18]= "Eighteen "; bits[19]= "Nineteen ";
        bits[20]= "Twenty "; bits[30]= "Thirty ";
        bits[40]= "Forty "; bits[50]= "Fifty ";
        bits[60]= "Sixty "; bits[70]= "Seventy ";
        bits[80]= "Eighty "; bits[90]= "Ninety ";

        x = groups.length-1;
        while(anyNumber.length() > 0) // convert in groups - 111,222,333
        {
            if(anyNumber.length() < 3)
            {
                groups[x] = Integer.parseInt(anyNumber);
                anyNumber = "";
            }
            else
            {
                groups[x] = Integer.parseInt(
                    anyNumber.substring(anyNumber.length()-3,anyNumber.length()));
                anyNumber = anyNumber.substring(0,anyNumber.length()-3);
            }
            x--;
        }

        for(i = x+1; i < groups.length; i++)
        {
            firstDigit = (int)groups[i]/100;
            secondDigit = (int)(groups[i] - (int)(groups[i]/100) * 100)/10;
            thirdDigit = groups[i] - (firstDigit * 100) - (secondDigit * 10);

            if(secondDigit == 1)
            {
                secondDigit += 9 + thirdDigit;
                thirdDigit = 0;
            }
            else secondDigit *= 10;
            // setting flag for "and" in third group
            flag = (i < groups.length-1 && (firstDigit + secondDigit + thirdDigit) > 0)? 1:flag;

            H = firstDigit > 0? 1 : 0;

            A = ((firstDigit > 0 || (i == groups.length-1 && flag == 1)) &&
                                (secondDigit > 0 || thirdDigit > 0))? 1 : 0;

            D = (firstDigit + secondDigit + thirdDigit) > 0? i : groups.length-1;

            text += bits[firstDigit] + hundred[H] + and[A] +
                bits[secondDigit] + bits[thirdDigit] + divisor[D]+"\n";
        }                                                                             //"\n" is optional
        return minus+text;
    }
}


back     top   main page    vb snippets page      java snippets page     vbscript snippets page   email    Page last modified