Java Convert Number to Words


 

Java Convert Number to Words

In this section, you will learn how to convert a numeric value into its equivalent words.

In this section, you will learn how to convert a numeric value into its equivalent words.

Java Convert Number to Words

In this section, you will learn how to convert a numeric value into its equivalent words. The words of the numbers(1-100) are generally accepted. However, once a number to be converted reaches a value of thousand, lakhs, crores or greater, the names of numbers vary according to the usage. Through the given code, you can convert the large digit numbers into words.

Here is the code:

import java.util.*;

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

	public String convert(int number) {
		int n = 1;
		int word;
		string = "";
		while (number != 0) {
			switch (n) {
			case 1:
				word = number % 100;
				pass(word);
				if (number > 100 && number % 100 != 0) {
					show("and ");
				}
				number /= 100;
				break;

			case 2:
				word = number % 10;
				if (word != 0) {
					show(" ");
					show(st2[0]);
					show(" ");
					pass(word);
				}
				number /= 10;
				break;

			case 3:
				word = number % 100;
				if (word != 0) {
					show(" ");
					show(st2[1]);
					show(" ");
					pass(word);
				}
				number /= 100;
				break;

			case 4:
				word = number % 100;
				if (word != 0) {
					show(" ");
					show(st2[2]);
					show(" ");
					pass(word);
				}
				number /= 100;
				break;

			case 5:
				word = number % 100;
				if (word != 0) {
					show(" ");
					show(st2[3]);
					show(" ");
					pass(word);
				}
				number /= 100;
				break;

			}
			n++;
		}
		return string;
	}

	public void pass(int number) {
		int word, q;
		if (number < 10) {
			show(st1[number]);
		}
		if (number > 9 && number < 20) {
			show(st3[number - 10]);
		}
		if (number > 19) {
			word = number % 10;
			if (word == 0) {
				q = number / 10;
				show(st4[q - 2]);
			} else {
				q = number / 10;
				show(st1[word]);
				show(" ");
				show(st4[q - 2]);
			}
		}
	}

	public void show(String s) {
		String st;
		st = string;
		string = s;
		string += st;
	}

	public static void main(String[] args) {
		NumToWords w = new NumToWords();
		Scanner input = new Scanner(System.in);
		System.out.print("Enter Number: ");
		int num = input.nextInt();
		String inwords = w.convert(num);
		System.out.println(inwords);
	}
}

Output:

Enter Number: 10550
ten thousand five hundred and fifty

Ads