Java Number to Word numberToWord(long number)

Here you can find the source of numberToWord(long number)

Description

number To Word

License

Open Source License

Declaration

public static String numberToWord(long number) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

    public static String numberToWord(long number) {
        if (number == 0) {
            return "zero";
        }/*from   ww w. j av  a 2  s .  c  o  m*/

        if (number < 0) {
            return "-" + numberToWord(Math.abs(number));
        }

        String words = "";

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

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

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

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

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

Related

  1. numberToWords(long value, boolean useAnd)