Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created March 3, 2011 08:35
Show Gist options
  • Select an option

  • Save LCamel/852511 to your computer and use it in GitHub Desktop.

Select an option

Save LCamel/852511 to your computer and use it in GitHub Desktop.
中文數字
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