Showing posts with label convert number to text. Show all posts
Showing posts with label convert number to text. Show all posts

Wednesday, November 20, 2013

java convert number to word (text)

/**
 *
 * @author Pritom K Mondal
 */
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class NumberToWords {
    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 static String convertLessThanOneThousand(int number) {
        String soFar;
        
        if (number % 100 < 20){
            soFar = numNames[number % 100];
            number /= 100;
        } else {
            soFar = numNames[number % 10];
            number /= 10;
            
            soFar = tensNames[number % 10] + soFar;
            number /= 10;
        }
        if (number == 0) return soFar;
        return numNames[number] + " hundred" + soFar;
    }

    private static String convert(long number) {
        // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }
        
        String snumber = Long.toString(number);
        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);
        // XXXnnnnnnnnn 
        int billions = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int millions  = Integer.parseInt(snumber.substring(3,6)); 
        // nnnnnnXXXnnn
        int hundredThousands = Integer.parseInt(snumber.substring(6,9)); 
        // nnnnnnnnnXXX
        int thousands = Integer.parseInt(snumber.substring(9,12));    
        
        String tradBillions;
        switch (billions) {
            case 0:
                tradBillions = "";
                break;
            case 1 :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
                break;
            default :
                tradBillions = convertLessThanOneThousand(billions) 
                        + " billion ";
        }
        String result =  tradBillions;
        
        String tradMillions;
        switch (millions) {
            case 0:
                tradMillions = "";
                break;
            case 1 :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
                break;
            default :
                tradMillions = convertLessThanOneThousand(millions) + " million ";
        }
        result =  result + tradMillions;
        
        String tradHundredThousands;
        switch (hundredThousands) {
            case 0:
                tradHundredThousands = "";
                break;
            case 1 :
                tradHundredThousands = "one thousand ";
                break;
            default :
                tradHundredThousands = convertLessThanOneThousand(hundredThousands)  + " thousand ";
        }
        result =  result + tradHundredThousands;
        
        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result =  result + tradThousand;
        
        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
    }
    
    public static String convert(String number, String weight) {
        String[] numbers = number.split("\\.");
        String[] weights = weight.split("\\.");
        String word = "";
        for(Integer position = 0; position < numbers.length; position++) {
            if(numbers[position].trim().length() > 0) {
                if(position == 1) {
                    word = word.trim();
                    if(word.startsWith("zero")) {
                        word = "";
                    } else {
                        word = word.trim() + " and ";
                    }
                }
                word += NumberToWords.convert(new Long("" + numbers[position]));
                if(weights.length >= position) {
                    word = word.trim() + " " + weights[position];
                }
            }
        }
        return word.trim().toUpperCase();
    }
    
    /**
     * testing
     * @param args
     */
    public static void main(String[] args) {
        List numberList = new ArrayList<String>();
        numberList.add(0);
        numberList.add(1);
        numberList.add(7);
        numberList.add(15.98);
        numberList.add("36.06");
        numberList.add(48);
        numberList.add(67.986);
        numberList.add(89.09);
        numberList.add(435);
        numberList.add(.110);
        numberList.add(328.64);
        numberList.add(99999);
        numberList.add(100000);
        numberList.add(1000010);
        numberList.add(1002);
        numberList.add(3567);
        numberList.add(6942);
        numberList.add(9073);
        numberList.add(4583);
        numberList.add(43768);
        numberList.add(35789);
        numberList.add(35483);
        numberList.add(468368);
        numberList.add(9073254);
        numberList.add(73859735);
        numberList.add("5890203983");
        numberList.add(3000000010L);
        
        Object[] objects = numberList.toArray();
        for(Integer position = 0; position < objects.length; position++) {
            System.out.println("" + 
                    NumberToWords.convert("" + objects[position], "DOLLAR.CENT") 
                    + " (" + objects[position] + ")");
        }
    }
}

Output:

ZERO DOLLAR (0)
ONE DOLLAR (1)
SEVEN DOLLAR (7)
FIFTEEN DOLLAR AND NINETY EIGHT CENT (15.98)
THIRTY SIX DOLLAR AND SIX CENT (36.06)
FORTY EIGHT DOLLAR (48)
SIXTY SEVEN DOLLAR AND NINE HUNDRED EIGHTY SIX CENT (67.986)
EIGHTY NINE DOLLAR AND NINE CENT (89.09)
FOUR HUNDRED THIRTY FIVE DOLLAR (435)
ELEVEN CENT (0.11)
THREE HUNDRED TWENTY EIGHT DOLLAR AND SIXTY FOUR CENT (328.64)
NINETY NINE THOUSAND NINE HUNDRED NINETY NINE DOLLAR (99999)
ONE HUNDRED THOUSAND DOLLAR (100000)
ONE MILLION TEN DOLLAR (1000010)
ONE THOUSAND TWO DOLLAR (1002)
THREE THOUSAND FIVE HUNDRED SIXTY SEVEN DOLLAR (3567)
SIX THOUSAND NINE HUNDRED FORTY TWO DOLLAR (6942)
NINE THOUSAND SEVENTY THREE DOLLAR (9073)
FOUR THOUSAND FIVE HUNDRED EIGHTY THREE DOLLAR (4583)
FORTY THREE THOUSAND SEVEN HUNDRED SIXTY EIGHT DOLLAR (43768)
THIRTY FIVE THOUSAND SEVEN HUNDRED EIGHTY NINE DOLLAR (35789)
THIRTY FIVE THOUSAND FOUR HUNDRED EIGHTY THREE DOLLAR (35483)
FOUR HUNDRED SIXTY EIGHT THOUSAND THREE HUNDRED SIXTY EIGHT DOLLAR (468368)
NINE MILLION SEVENTY THREE THOUSAND TWO HUNDRED FIFTY FOUR DOLLAR (9073254)
SEVENTY THREE MILLION EIGHT HUNDRED FIFTY NINE THOUSAND SEVEN HUNDRED THIRTY FIVE DOLLAR (73859735)
FIVE BILLION EIGHT HUNDRED NINETY MILLION TWO HUNDRED THREE THOUSAND NINE HUNDRED EIGHTY THREE DOLLAR (5890203983)
THREE BILLION TEN DOLLAR (3000000010)