【阿拉伯數字轉英文大寫】Java實現

One common security method requires that the amount on acheque be written in numbers and spelled out in words as well. Even if someone is able to alter the numerical amount of the check, it is extremely difficult to change the amount in words. Write an application that asks the user to input a numeric check amount and writes the word equivalent of the amount. For example, the amount 112.43 should be written as ONE hundred TWELVE and 43/100. A sample run:

java.util.StringTokenizer will be useful in developing the above application.
Util.java

package com.zx.money;

/**
 * @author 作者
 * @version 創建時間:2019年10月19日 上午10:21:42
 * @email [email protected] 類說明
 */
public class Util {

	static StringBuffer dealThree(int num) {// 處理三位數103
		StringBuffer sb = new StringBuffer();
		if (num / 100 == 0) {// 例:010,001
			int cache = (num % 100) / 10;// 獲得中間位數
			if (num % 100 <= 20 || num % 10 == 0) {// 如果該數爲1-20間的數或者爲幾十的數
				sb.append(getUp(num));
			} else {// 030
				cache = (num % 100) - (num % 10);// 得到80
				sb.append(getUp(cache));
				sb.append("-");
				sb.append(getUp(num % 10));
			}
		} else {// 120,h=1
			int h = num / 100;
			sb.append(getUp(h) + " hundred ");
			int cache = (num % 100) / 10;// 判斷中間的數是否爲零
			if (cache == 0) {// 如果爲零
				if (num % 100 == 0) {// 整百
					sb.append(getUp(num % 100));
				} else {// 非整百
					sb.append(getUp(num % 10));
					sb.append(" and ");
				}
			} else {// 不爲零 ex:123
				if (num % 100 <= 20 || num % 10 == 0) {// 如果該數爲1-20間的數或者爲幾十的數 num == 120
					sb.append(" and ");
					sb.append(getUp(num % 100));
				} else {// ex: 183
					cache = (num % 100) - (num % 10);// 得到80
					sb.append(" and ");
					sb.append(getUp(cache));
					sb.append("-");
					sb.append(getUp(num % 10));
				}
			}
		}
		return sb;
	}


	public static String billion(int num) {
		million(num);
		return null;
	}
	public static String million(int num) {
		thousand(num);
		return null;
	}
	public static String thousand(int num) {
		hundred(num);
		return null;
	}
	public static String hundred(int num) {
		return null;
	}
	public static String getUp1(int num) {
		String str = "";
		switch (num) {
		case 1:
			str = "one";
			break;
		case 2:
			str = "two";
			break;
		case 3:
			str = "three";
			break;
		case 4:
			str = "four";
			break;
		case 5:
			str = "five";
			break;
		case 6:
			str = "six";
			break;
		case 7:
			str = "seven";
			break;
		case 8:
			str = "eight";
			break;
		case 9:
			str = "nine";
			break;
		case 10:
			str = "ten";
			break;
		case 11:
			str = "elevn";
			break;
		case 12:
			str = "twelve";
			break;
		case 13:
			str = "thirteen";
			break;
		case 14:
			str = "fourteen";
			break;
		case 15:
			str = "fifteen";
			break;
		case 16:
			str = "sixteen";
			break;
		case 17:
			str = "seventeen";
			break;
		case 18:
			str = "eighteen";
			break;
		case 19:
			str = "nineteen";
			break;
		case 20:
			str = "twenty";
			break;
		case 30:
			str = "thirty";
			break;
		case 40:
			str = "forty";
			break;
		case 50:
			str = "fifty";
			break;
		case 60:
			str = "sixty";
			break;
		case 70:
			str = "seventy";
			break;
		case 80:
			str = "eighty";
			break;
		case 90:
			str = "ninety";
			break;
		default:
			break;
		}
		return str;
	}

	public static String getUp(int num) {
		String str = "";
		switch (num) {
		case 1:
			str = "ONE";
			break;
		case 2:
			str = "TWO";
			break;
		case 3:
			str = "THREE";
			break;
		case 4:
			str = "FOUR";
			break;
		case 5:
			str = "FIVE";
			break;
		case 6:
			str = "SIX";
			break;
		case 7:
			str = "SEVEN";
			break;
		case 8:
			str = "EIGHT";
			break;
		case 9:
			str = "NINE";
			break;
		case 10:
			str = "TEN";
			break;
		case 11:
			str = "ELEVN";
			break;
		case 12:
			str = "TWELVE";
			break;
		case 13:
			str = "THIRTEEN";
			break;
		case 14:
			str = "FOURTEEN";
			break;
		case 15:
			str = "FIFTEEN";
			break;
		case 16:
			str = "SIXTEEN";
			break;
		case 17:
			str = "SEVENTEEN";
			break;
		case 18:
			str = "EIGHTEEN";
			break;
		case 19:
			str = "NINETEEN";
			break;
		case 20:
			str = "TWENTY";
			break;
		case 30:
			str = "THIRTY";
			break;
		case 40:
			str = "FORTY";
			break;
		case 50:
			str = "FIFTY";
			break;
		case 60:
			str = "SIXTY";
			break;
		case 70:
			str = "SEVENTY";
			break;
		case 80:
			str = "EIGHTY";
			break;
		case 90:
			str = "NINETY";
			break;
		default:
			break;
		}
		return str;
	}
}

Money.java

package com.zx.money;

import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * @author 作者
 * @version 創建時間:2019年10月19日 上午9:55:58
 * @email [email protected] 類說明
 */
public class Money {
	public static void main(String[] args) {
		System.out.println("Please enter a dollar amount:");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String arr[] = new String[2];
		StringBuffer sb = new StringBuffer();//
		int m = 0;
		StringTokenizer st = new StringTokenizer(str, ".");
		while (st.hasMoreTokens()) {// 將輸入的數切成整數和小數兩部分
			arr[m] = (String) st.nextElement();
			m++;
		}
		int length = arr[0].length();
		// int num = Integer.parseInt(arr[0]);
		int threeNum = length / 3;// 幾個三位數組成
		int firstNum = length % 3;
		switch (threeNum) {
		case 4:
			StringBuffer s = new StringBuffer();
			s.append(arr[0]);
			String substring = s.substring(0, 3);
			int four = Integer.parseInt(substring);
			sb.append(Util.dealThree(four));
			sb.append(" billion ");
			int num = Integer.parseInt(s.substring(3));
			sb.append(Util.dealThree(num / 1000000));// num/1000000
			System.out.println(num / 1000000);
			sb.append(" million ");
			sb.append(Util.dealThree((num % 1000000) / 1000));// num 123000789
			if (((num % 1000000) - (num % 1000)) != 0) {
				sb.append(" thousand ");
			} else if (((num % 1000000) - (num % 100)) == 0 || ((num % 1000000) - (num % 10)) == 0) {
				sb.append(" and ");
			}
			sb.append(Util.dealThree(num % 1000));
			break;
		case 3://
			num = 0;
			if (firstNum != 0) {// 11123456789
				StringBuffer newSb = new StringBuffer();
				newSb.append(arr[0].charAt(0));
				if (firstNum == 2) {
					newSb.append(arr[0].charAt(1));
				}
				int first = Integer.parseInt(newSb.toString());
				sb.append(Util.dealThree(first));
				sb.append(" billion ");
				num = Integer.parseInt(arr[0].substring(2));
				sb.append(Util.dealThree(num / 1000000));// num/1000000
				// System.out.println(num / 1000000);
				sb.append(" million ");
				sb.append(Util.dealThree((num % 1000000) / 1000));// num 123000789
				if (((num % 1000000) - (num % 1000)) != 0) {
					sb.append(" thousand ");
				} else if (((num % 1000000) - (num % 100)) == 0 || ((num % 1000000) - (num % 10)) == 0) {
					sb.append(" and ");
				}
				sb.append(Util.dealThree(num % 1000));
			} else {// 正好是三的倍數個 位數,例如123456789
				sb.append(Util.dealThree(num / 1000000));// num/1000000
				System.out.println(num / 1000000);
				sb.append(" million ");
				sb.append(Util.dealThree((num % 1000000) / 1000));// num 123000789
				if (((num % 1000000) - (num % 1000)) != 0) {
					sb.append(" thousand ");
				} else if (((num % 1000000) - (num % 100)) == 0 || ((num % 1000000) - (num % 10)) == 0) {
					sb.append(" and ");
				}
				sb.append(Util.dealThree(num % 1000));
			}
			break;
		case 2:// 100000-99 999 999

			num = Integer.parseInt(arr[0]);
			if (firstNum == 0) {// 正好是三的倍數個位數,例如100006
				sb.append(Util.dealThree((num % 1000000) / 1000));
				sb.append(" thousand ");
				if ((num % 1000 - num % 100) == 0) {// 判斷百位是否爲零,如果爲零,加and ex. 120030
					sb.append(" and ");
				}
			} else {// 七位或者八位
				StringBuffer newSb = new StringBuffer();
				newSb.append(arr[0].charAt(0));
				if (firstNum == 2) {
					newSb.append(arr[0].charAt(1));
				}
				int first = Integer.parseInt(newSb.toString());
				sb.append(Util.dealThree(first));
				sb.append(" million ");
				sb.append(Util.dealThree((num % 1000000) / 1000));
				sb.append(" thousand ");
				if ((num % 1000 - num % 100) == 0) {// 判斷百位是否爲零,如果爲零,加and ex. 120030
					sb.append(" and ");
				}
			}
			sb.append(Util.dealThree(num % 1000));
			break;
		case 1:
			num = Integer.parseInt(arr[0]);
			if (arr[0].length() % 3 == 0) {
				int h = num / 100;
				sb.append(Util.getUp(h) + " hundred ");
				int cache = (num % 100) / 10;// 判斷中間的數是否爲零
				if (cache == 0) {// 如果爲零
					int last = num % 10;// 最後一位
					sb.append(" and ");
					sb.append(Util.getUp(last));
				} else {// 不爲零 ex:123
					if (num % 100 <= 20 || num % 10 == 0) {// 如果該數爲1-20間的數或者爲幾十的數 num == 120
						sb.append(" and ");
						sb.append(Util.getUp(num % 100));
					} else {// ex: 183
						cache = (num % 100) - (num % 10);// 得到80
						sb.append(" and ");
						sb.append(Util.getUp(cache));
						sb.append("-");
						sb.append(Util.getUp(num % 10));
					}
				}
			}
			else {
				StringBuffer newSb = new StringBuffer();
				newSb.append(arr[0].charAt(0));
				if (firstNum == 2) {
					newSb.append(arr[0].charAt(1));
				}
				int first = Integer.parseInt(newSb.toString());
				sb.append(Util.dealThree(first));
				sb.append(" thousand ");
				int h = num / 100;
				sb.append(Util.getUp(h) + " hundred ");
				int cache = (num % 100) / 10;// 判斷中間的數是否爲零
				if (cache == 0) {// 如果爲零
					int last = num % 10;// 最後一位
					sb.append(" and ");
					sb.append(Util.getUp(last));
				} else {// 不爲零 ex:123
					if (num % 100 <= 20 || num % 10 == 0) {// 如果該數爲1-20間的數或者爲幾十的數 num == 120
						sb.append(" and ");
						sb.append(Util.getUp(num % 100));
					} else {// ex: 183
						cache = (num % 100) - (num % 10);// 得到80
						sb.append(" and ");
						sb.append(Util.getUp(cache));
						sb.append("-");
						sb.append(Util.getUp(num % 10));
					}
				}
			}
			break;
		case 0:// 兩位或者一位數
			num = Integer.parseInt(arr[0]);
			sb.append(Util.dealThree(num));
			break;
		default:
			break;
		}
		sb.append(" and ").append(arr[1]).append("/100");
		System.out.println(sb.toString());
	}

}

功能已實現,但過程太冗餘,其實可以六位調用三位的處理方法,九位調用六位的處理方法……

過段時間上傳改進版。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章