Logic to rewrite numbers as words using JAVA

Very often we come across certain situations for converting numbers as words in generating invoice or bills applications. Here I am providing the logic in JAVA for doing this task.

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

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

    public String numtoword(int number, boolean alpha, boolean uppercase) {
        String result = "";
        if (!alpha) {
            return "" + number + "";
        }
        if (number % 100 < 20) {
            result = one_to_nineteen[number];
            number /= 100;
        } else {
            result = one_to_nineteen[number % 10];
            result = result == "" ? tensnames[number % 10] + result : tensnames[(number /= 10) % 10] + "-" + result;
            number /= 10;
        }
        if (number == 0) {
            if (uppercase) {
                return this.ToUppercase(result);
            }
            return result;
        }
        result = number / 10 == 0 ? one_to_nineteen[number % 10] + " hundred " + result : one_to_nineteen[number % 10] + " hundred and " + result;
        if ((number /= 10) == 0) {
            if (uppercase) {
                return this.ToUppercase(result);
            }
            return result;
        }
        if (number < 20) {
            result = one_to_nineteen[number] + " thousand " + result;
        } else {
            result = one_to_nineteen[number % 10] + " thousand " + result;
            result = tensnames[number /= 10] + "-" + result;
        }
        if (uppercase) {
            return this.ToUppercase(result);
        }
        return result;
    }

    public String ToUppercase(String result) {
        char firstindex = Character.toUpperCase(result.charAt(0));
        String resultUpper = result.substring(1, result.length());
        return "" + firstindex + resultUpper;
    }

Now, call the NumToword() method from anywhere in the application and pass the required parameters. This methods returns the Currency or number in word format.

Happy Coding.