Created
March 3, 2011 08:35
-
-
Save LCamel/852511 to your computer and use it in GitHub Desktop.
中文數字
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static final char[] digits = new char[] { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' }; | |
| private static final Character[] little = new Character[] { null, '十', '百', '千' }; | |
| private static final Character[] big = new Character[] { null, '萬', '億', '兆', '京' }; | |
| private static void out(Character c) { | |
| if (c == null) | |
| return; | |
| System.out.println(c); | |
| } | |
| private static void chineseNumbers(long n) { | |
| int pow = 0; | |
| Character pendingBig = 42; | |
| boolean nz2z = false; | |
| while (n > 0) { | |
| if (pow % 4 == 0) | |
| pendingBig = big[pow / 4]; | |
| int d = (int) (n % 10); | |
| n = n / 10; | |
| if (d != 0) { | |
| if (pendingBig != null) { | |
| out(pendingBig); | |
| pendingBig = null; | |
| } | |
| out(little[pow % 4]); | |
| if (!(n == 0 && d == 1 && pow % 4 == 1)) // 一十 開頭的話, 去掉 一 | |
| out(digits[d]); | |
| nz2z = true; | |
| } else { | |
| if (nz2z) | |
| out(digits[d]); | |
| nz2z = false; | |
| } | |
| pow++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment