一、阿拉伯数字与大写金额互换
#region 阿拉伯数字与大写金额互换
/// <summary>
/// 将阿拉伯数字转为大写金额
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static String ConvertToChinese(Decimal number)
{
number = number * 10000;
var s = number.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");
var 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}");
var r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
return r;
}
/// <summary>
/// 中文金额转换为数字金额(不包含负金额)
/// </summary>
/// <param name="chineseAmount">中文金额</param>
/// <returns>数字金额</returns>
public static string ChineseConvertToNumber(string chineseAmount)
{
if (string.IsNullOrEmpty(chineseAmount))
{
return string.Empty;
}
chineseAmount.Replace("零", "").Replace("元", "").Replace("整", "");//移除计算干扰文字
var wordCharArray = chineseAmount.ToCharArray();
double numberAmount = 0;//最终要返回的数字金额
//金额位标志量
bool wan = false;//表示有万位
bool yi = false;//表示有亿位
bool fen = false;//表示有分位
bool jiao = false;//表示有角位
bool shi = false;//表示有十位
bool bai = false;//表示有百位
bool qian = false;//表示有千位
for (int i = (wordCharArray.Length - 1); i >= 0; i--)//从低位到高位计算
{
double currentPlaceAmount = 0;//当前位金额值
//判断当前位对应金额标志量
if (wordCharArray[i] == '分')
{
fen = true;
continue;
}
else if (wordCharArray[i] == '角')
{
jiao = true;
fen = false;
continue;
}
else if (wordCharArray[i] == '拾')
{
fen = false;
jiao = false;
shi = true;
continue;
}
else if (wordCharArray[i] == '佰')
{
bai = true;
fen = false;
jiao = false;
shi = false;
continue;
}
else if (wordCharArray[i] == '仟')
{
qian = true;
fen = false;
jiao = false;
shi = false;
bai = false;
continue;
}
else if (wordCharArray[i] == '万')
{
wan = true;
fen = false;
jiao = false;
shi = false;
bai = false;
qian = false;
continue;
}
else if (wordCharArray[i] == '亿')
{
yi = true;
wan = false;
fen = false;
jiao = false;
shi = false;
bai = false;
qian = false;
continue;
}
//根据标志量转换金额为实际金额
if (fen) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 0.01;
else if (jiao)
{
currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 0.1;
jiao = false;
}
else if (shi) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 10;
else if (bai) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 100;
else if (qian) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 1000;
else
{
currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]);
}
//每万位处理
if (yi)
{
currentPlaceAmount = currentPlaceAmount * 100000000;
}
else if (wan)
{
currentPlaceAmount = currentPlaceAmount * 10000;
}
numberAmount += currentPlaceAmount;
}
return numberAmount.ToString();
}
/// <summary>
/// 转换中文数字为阿拉伯数字
/// </summary>
/// <param name="chinese">中文数字</param>
/// <returns></returns>
private static int ConvertNameToSmall(char chinese)
{
int number = 0;
switch (chinese.ToString())
{
case "零": number = 0; break;
case "壹": number = 1; break;
case "贰": number = 2; break;
case "叁": number = 3; break;
case "肆": number = 4; break;
case "伍": number = 5; break;
case "陆": number = 6; break;
case "柒": number = 7; break;
case "捌": number = 8; break;
case "玖": number = 9; break;
default: throw new Exception("中文金额数字不正确:" + chinese);
}
return number;
}
/// <summary>
/// 阿拉伯数字转中文
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public static string NumericToZH(string index)
{
var indexInt = ToInt(index);
if (indexInt > 10)
{
return string.Empty;
}
string[] num = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
return num[indexInt];
}
#endregion