Generate english phrase that describes a integer

Given an integer between 0 and 999,999, print an English phrase that describes the integer (e.g., “One Thousand, Two Hundred and Thirty Four”).

My initial codes:

	private static final Map<Integer, String> digitsToStrings;
	static {
		Map<Integer, String> aMap = new HashMap<Integer, String>();

		aMap.put(1, "One");
		aMap.put(2, "Two");
		aMap.put(3, "Three");
		aMap.put(4, "Four");
		aMap.put(5, "Five");
		aMap.put(6, "Six");
		aMap.put(7, "Seven");
		aMap.put(8, "Eight");
		aMap.put(9, "Nine");

		aMap.put(10, "Ten");
		aMap.put(11, "Eleven");
		aMap.put(12, "Twelve");
		aMap.put(13, "Thirteen");
		aMap.put(14, "Fourteen");
		aMap.put(15, "Fivteen");
		aMap.put(16, "Sixteen");
		aMap.put(17, "Seventeen");
		aMap.put(18, "Eighteen");
		aMap.put(19, "Nineteen");

		aMap.put(20, "Twenty");
		aMap.put(30, "Thirty");
		aMap.put(40, "Fourty");
		aMap.put(50, "Fifty");
		aMap.put(60, "Sixty");
		aMap.put(70, "Seventy");
		aMap.put(80, "Eighty");
		aMap.put(90, "Ninty");

		digitsToStrings = Collections.unmodifiableMap(aMap);
	}

	// number is between 0 to 999
	private static String hundredToString(int number) {
		String result = new String();
		int numberOfHandred = number / 100;
		result += numberOfHandred == 0 ? "" : digitsToStrings
				.get(numberOfHandred) + " Hundred";
		int residual = number - numberOfHandred * 100;
		if (residual == 0)
			return result;
		else if (numberOfHandred != 0)
			result += " and ";
		int numberOfTens = residual / 10;

		switch (numberOfTens) {
		case 0: {
			result += "";
			break;
		}
		case 1: {
			result += digitsToStrings.get(residual);
			return result;
		}
		default: {
			result += numberOfTens == 0 ? "" : digitsToStrings
					.get(numberOfTens * 10) + "";
			break;
		}
		}
		int numberOfOnes = residual - numberOfTens * 10;
		result += numberOfOnes == 0 ? "" : " "
				+ digitsToStrings.get(numberOfOnes);
		return result;
	}

	// number is between 0 to 999,999
	public static String numberToString(int number) {
		int upper_half = number / 1000;
		int lower_half = number - upper_half * 1000;
		if (upper_half == 0)
			return hundredToString(lower_half);
		else
			return hundredToString(upper_half) + " Thousand, "
					+ hundredToString(lower_half);
	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: