C#小写数字金额转换成大写人民币金额的算法
第一种方法:
using System.Text.RegularExpressions;//首先引入命名空间
private string DaXie(string money)
{
string s = double.Parse(money).ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
return Regex.Replace(d, ".", delegate(Match m) { return "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
}
第二种方法:
public string chang(string money)
{
//将小写金额转换成大写金额
double MyNumber = Convert.ToDouble (money );
String[] MyScale = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" }; //定义单位字符串数组
String[] MyBase = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; //定义数字字符串数组
String M ="";
bool isPoint =false;
if (money.IndexOf(".") !=-1)
{
money = money.Remove(money.IndexOf("."), 1);
isPoint =true;
}
for (int i = money.Length; i >0; i--)
{
int MyData = Convert.ToInt16(money[money.Length - i].ToString ());//获取money中的数字,并转换为浮点型
M += MyBase[MyData];//从mybase中取出同index的数字的汉字字符
if (isPoint==true)
{
M += MyScale[i -1];//获取单位
}
else
{
M += MyScale[i +1];//获取单位
}
}
return M; //返回转换的金额字符串
}