Number to its equivalent english phrase problem.

Given a floating point number (that represents money), print an English phrase which describes that quantity of money.

eg. 1234.39 -> one thousand two hundred thirty four dollars and thirty nine cents

This particular problem is one of those problems that are not that hard but are just so tedious, this problem is also very common in programming interviews so check out my solution and make sure you understand how it works.

Solution:

The key is to realize that there is a pattern, each three digits we have the same structure:

hundreds – tens – digits

so we can build our string as follows

23,456,792 -> convert(23) + ” million ” +¬†convert(456) + ” thousand ” + convert (792) + ” ”

code:

import java.util.*;

public class NumberToString
{
	static final String thous[] = new String[]{"","thousand"};
	static final String bigs[] = new String[]{"","million", "billion"};
	static final String numbers[] = new String[100];

	public static void main(String... args)
	{
		numbers[1] = "one";
		numbers[2] = "two";
		numbers[3] = "three";
		numbers[4] = "four";
		numbers[5] = "five";
		numbers[6] = "six";
		numbers[7] = "seven";
		numbers[8] = "eight";
		numbers[9] = "nine";
		numbers[10] = "ten";
		numbers[11] = "eleven";
		numbers[12] = "twelve";
		numbers[13] = "thirteen";
		numbers[14] = "fourteen";
		numbers[15] = "fifteen";
		numbers[16] = "sixteen";
		numbers[17] = "seventeen";
		numbers[18] = "eighteen";
		numbers[19] = "nineteen";
		numbers[20] = "twenty";
		numbers[30] = "thirty";
		numbers[40] = "forty";
		numbers[50] = "fifty";
		numbers[60] = "sixty";
		numbers[70] = "seventy";
		numbers[80] = "eighty";
		numbers[90] = "ninety";
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextDouble())
			System.out.println(convert(sc.nextDouble()));

	}
	static String convert(double n)
	{
		long decimals = getDecimals(n);
		long integer = (long) n;
		String result = "";
		int count = 0;
		int count2 = 0;

		if(integer == 0)
			result = "zero ";

		while(integer > 0)
		{
			long chunk = integer % 1000000;
			count = 0;
			boolean add = chunk > 0;
			String t = "";

			while(chunk > 0)
			{
				String temp = convertDigits(chunk % 1000);

				if(!temp.isEmpty())
					t = temp + thous[count] + " " + t + " ";

				count++;
				chunk /= 1000;
			}

			if(add)
				result = t.trim() + " " + bigs[count2] + " " + result + " ";

			integer /= 1000000;
			count2++;
		}

		String cents = convertDigits(decimals) +"cents";

		return result.trim() + " dollars " + (decimals > 0 ? "and " + cents : "");
	}
	static String convertDigits(long num)
	{
		String result = "";

		if(num / 100 > 0)
			result += numbers[(int)(num / 100)] + " hundred ";

		num = num % 100;

		if(num  0)
		{
			result += numbers[(int)num]+" ";
			return result;
		}

		if(num / 10 > 0)
			result += numbers[(int)(num/10 * 10)]+" ";

		num = num % 10;
		if(num > 0)
			result += numbers[(int)num]+" ";

		return result;
	}
	static long getDecimals(double n)
	{
		return (long) (n * 100.0) % 100;
	}
}
Advertisements