C#实现人民币大写转换示例代码
/// <summary>
/// 人民币大写
/// </summary>
/// <param name="input">待转换输入</param>
/// <param name="prefix">需要添加人民币前缀</param>
/// <exception cref="argumentexception" />
/// <returns>转换后的结果</returns>
public static string tocapital(this string input, bool prefix = false) {
#region step1 输入有效性验证
if (!regex.ismatch(input, @"(?<=-|^)\d*\.?\d*$"))
throw new argumentexception("错误的输入金额!");
if (regex.ismatch(input, @"^\d{25,}"))
throw new argumentexception("输入数据太大无法转换!");
#endregion
#region step2 格式化为中间字符串
var positiondic = new dictionary<int, string> {
{0,"f"},{1,"j"},{2,"."},{3,"s"},{4,"b"},{5,"q"},{6,"w"},{7,"sw"},{8,"bw"},{9,"qw"},
{10,"y"},{11,"sy"},{12,"by"},{13,"qy"},{14,"wy"},{15,"swy"},{16,"bwy"},{17,"qwy"},{18,"yy"},
{19,"syy"},{20,"byy"},{21,"qyy"},{22,"wyy"},{23,"swyy"},{24,"bwyy"},{25,"qwyy"}
};
input = regex.replace(input, @"^\.", "0.");
var integerpart = regex.replace(input, @"^-|\..*$", "");
var _matchdecimal = regex.match(input, @"\.\d*$", regexoptions.none);
var decimalpart = regex.replace(_matchdecimal.success ? convert.todouble(_matchdecimal.value).tostring("0.00") : "00", @"0\.", "");
var processstack = new stack<string>();
var charsarray = (integerpart + decimalpart).reverse<char>();
for (int i = 0; i < charsarray.count(); i++) {
processstack.push(string.format("{0}{1}", charsarray.elementat(i), positiondic[i]));
}
//符号处理 keleyi.com
if (regex.ismatch(input, "^-", regexoptions.none)) {
processstack.push("-");
}
if (prefix) {
processstack.push("¥");
}
var process = string.empty;
while (processstack.count > 0) {
process += processstack.pop();
}
//语义处理模式队列
queue<tuple<string, string, matchevaluator>> patterns = new queue<tuple<string, string, matchevaluator>>();
var patternbuilder = new stringbuilder();
for (int i = 3; i < positiondic.count; i++) {
patternbuilder.appendformat("{0}{1}", (i == 3 ? "(0(?:" : "") + positiondic[i], i == positiondic.count - 1 ? ")+?)+" : "|");
}
patterns.enqueue(tuple.create<string, string, matchevaluator>(patternbuilder.tostring(), "0", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"(?:\d+(?:qw|bw|sw|w|q|b|s)?\d?yy)+", null, m => m.value.replace("yy", "") + "yy"));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"(?:\d+(?:qw|bw|sw|w|q|b|s)?\d?y)+", null, m => m.value.replace("y", "") + "y"));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"(?:\d+(?:q|b|s)?\d?w)+", null, m => m.value.replace("w", "") + "w"));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"(?!^)0+\.", ".", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"^0\.0j|^0\.", "", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>("0j|0f", "0", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>("j0?$", "jz", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"\.$|\.0+$", ".z", null));
patterns.enqueue(tuple.create<string, string, matchevaluator>(@"^0+$|^[^.]{0}$", "0.z", null));
while (patterns.count > 0) {
var pattern = patterns.dequeue();
if (pattern.item3 != null) {
process = regex.replace(process, pattern.item1, pattern.item3);
}
else {
process = regex.replace(process, pattern.item1, pattern.item2);
}
}
#endregion
#region step3 翻译中间字符串
stringbuilder result = new stringbuilder();
var translatordic = new dictionary<char, string> {
{'0',"零"},{'1',"壹"},{'2',"贰"},{'3',"叁"},{'4',"肆"},{'5',"伍"},{'6',"陆"},{'7',"柒"},{'8',"捌"},{'9',"玖"},
{'s',"拾"},{'b',"佰"},{'q',"仟"},{'w',"萬"},{'y',"亿"},
{'¥',"人民币"},{'-',"负"},{'.',"圆"},{'j',"角"},{'f',"分"},{'z',"整"}
};
for (int i = 0; i < process.length; i++) {
result.append(translatordic[process[i]]);
}
#endregion
return result.tostring();
}
上一篇: MySQL笔记之别名的使用