Created
June 7, 2016 08:33
-
-
Save hsinjungwu/a204cd8aff5e39d7b161589317dfdcdc 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
using System; | |
/* | |
INPUT : 11000904 | |
OUTPUT : 壹仟壹佰萬零玖佰零肆圓整 | |
*/ | |
private static string MoneyConvert(int moneySource) | |
{ | |
string[] chtInt = new string[] { "零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖" }; | |
string[] unit1 = new string[] { string.Empty, "拾", "佰", "仟" }; | |
string[] unit2 = new string[] { "圓整", "萬", "億", "兆" }; | |
if (moneySource == 0) return "零圓整"; | |
string result = string.Empty; | |
int widx = 0; | |
while (moneySource > 0) | |
{ | |
int idx = 0; | |
string s = string.Empty; | |
int money = moneySource % 10000; | |
while (money % 10 == 0 && money > 0) | |
{ | |
money /= 10; | |
idx++; | |
} | |
while (money > 0) | |
{ | |
int digit = money % 10; | |
if (idx == 0) | |
{ | |
if (digit > 0) s = chtInt[digit]; | |
} | |
else | |
s = chtInt[digit] + (digit > 0 ? unit1[idx] : string.Empty) + s; | |
money /= 10; | |
idx++; | |
} | |
if (!s.Contains(unit1[3])) s = chtInt[0] + s; | |
result = s.Replace("零零", "零") + unit2[widx] + result; | |
widx++; | |
moneySource /= 10000; | |
} | |
if (result[0].ToString() == chtInt[0]) | |
result = result.Substring(1); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment