C# 金钱 小写转大写的算法

调用 ConvertMoney的ConvertMoneyToWords(decimal money)方法即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Common
{
public class ConvertMoney
{
static string[] c_Num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
static string[] c_FH = { "", "拾", "佰", "仟" };
static string[] c_Wn = { "圆", "万", "亿" };
public static string ConvertMoneyToWords(decimal money)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(money.ToString()))
{
string[] moneysplit = money.ToString().Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
Regex reg = new Regex("零{2,}");
string m = moneysplit[];
int mlen = m.Length;
string word = string.Empty;
for (int i = mlen; i > ; i--)
{
word = ConverNumToWord(word, m[mlen - i], i, true);
}
if (moneysplit.Length > )
{
string d = moneysplit[];
int dlen = d.Length > ? : d.Length;
if (dlen == && d[] == '' && d[] == '')
{
word += "整";
}
else
{
for (int i = ; i < dlen; i++)
{
word = ConverNumToWord(word, d[i], i, false);
}
}
}
else
{
word += "整";
}
result = reg.Replace(word.ToString(), "零");
}
return result;
}
private static string ConverNumToWord(string appendStr, char a, int index, bool isYuan)
{
string s = c_Num[Convert.ToInt32(a) - ];
if (isYuan)
{
int z = (index - ) / ;
int y = (index - ) % ;
appendStr = appendStr + s + (s != "零" ? c_FH[y] : "");
if (y == )
{
appendStr = appendStr.Trim('零') + c_Wn[z];
}
}
else
{
if (index == && s != "零")
{
appendStr = appendStr + s + "角";
}
else if (index == )
{
appendStr = appendStr + s;
}
if (index == && s != "零")
{
appendStr = appendStr + s + "分";
}
}
return appendStr;
}
}
}
上一篇:【Linux相识相知】计算机的组成、linux发行版和哲学思想、基础命令和目录结构(FHS)


下一篇:【linux】自定义配置debian+openbox