using System;
namespace OCRTest
{
/// <summary>
/// C#金额大写转小写
/// </summary>
public static class RMBChineseConvertToNumber
{
/// <summary>
/// 将传入的大写金额字符串转换为小写的阿拉伯数字
/// </summary>
/// <param name="str">传入的字符串</param>
/// <returns></returns>
public static int BigChineseConvertToSmall(string str)
{
int totalCount = 0;//所有数据算出来的和
int beforeMillion = 0;//亿以前的和
int beforeWan = 0;//亿-万之间的和
int AfterWan = 0;//万以后的和
int Onehundredmillion = 100000000;
str = str.Replace("零", "").Replace("元", "").Replace("整", "");
if (!string.IsNullOrEmpty(str))
{
if (str.Contains("亿"))
{
string beforeyi = "";
string afteryi = "";
string[] arryi = str.Split(new string[] { "亿" }, StringSplitOptions.RemoveEmptyEntries);
beforeyi = arryi[0];
beforeMillion = beforeyi.ConvertNameToSmall() * Onehundredmillion;
if (arryi.Length == 1) { afteryi = string.Empty; } else { afteryi = arryi[1]; }
if (afteryi.Contains("万"))
{
string beforewan = "";
string afterwan = "";
string[] arrwan = afteryi.Split(new string[] { "万" }, StringSplitOptions.RemoveEmptyEntries);
beforewan = arrwan[0];
if (arrwan.Length == 1) { afterwan = string.Empty; } else { afterwan = arrwan[1]; }
beforeWan = GetTotalCount(beforeWan, beforewan, "before");
AfterWan = GetTotalCount(AfterWan, afterwan);
}
else
{
AfterWan = GetTotalCount(AfterWan, afteryi);
}
}
else if (str.Contains("万"))
{
string beforewan = "";
string afterwan = "";
string[] arrwan = str.Split(new string[] { "万" }, StringSplitOptions.RemoveEmptyEntries);
beforewan = arrwan[0];
if (arrwan.Length == 1) { afterwan = string.Empty; } else { afterwan = arrwan[1]; }
beforeWan = GetTotalCount(beforeWan, beforewan, "before");
AfterWan = GetTotalCount(AfterWan, afterwan);
}
else
{
AfterWan = GetTotalCount(AfterWan, str);
}
}
//最后计算出总和
totalCount = beforeMillion + beforeWan + AfterWan;
return totalCount;
}
/// <summary>
/// 将大写的中文名称转换为阿拉伯数字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int ConvertNameToSmall(this string str)
{
int number = 0;
switch (str)
{
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: break;
}
return number;
}
/// <summary>
/// 提取计算的公共方法
/// </summary>
/// <param name="direction">万以前或者万以后</param>
/// <param name="content">处理的内容</param>
/// <param name="dir">"before" 会*10000</param>
/// <returns></returns>
public static int GetTotalCount(int direction, string content, string dir = null)
{
int wan = 10000;
if (dir != "before") { wan = 1; };
if (content.Contains("仟"))
{
string beforeqian = "";
string afterqian = "";
string[] arrqian = content.Split(new string[] { "仟" }, StringSplitOptions.RemoveEmptyEntries);
beforeqian = arrqian[0];
if (arrqian.Length == 1) { afterqian = string.Empty; } else { afterqian = arrqian[1]; }
direction += beforeqian.ConvertNameToSmall() * 1000 * wan;
if (afterqian.Contains("佰"))
{
string beforebai = "";
string afterbai = "";
string[] arrbai = afterqian.Split(new string[] { "佰" }, StringSplitOptions.RemoveEmptyEntries);
beforebai = arrbai[0];
if (arrbai.Length == 1) { afterbai = string.Empty; } else { afterbai = arrbai[1]; }
direction += beforebai.ConvertNameToSmall() * 100 * wan;
if (afterbai.Contains("拾"))
{
string beforeshi = "";
string aftershi = "";
string[] arrshi = afterbai.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
beforeshi = arrshi[0];
if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
direction += beforeshi.ConvertNameToSmall() * 10 * wan;
aftershi = arrshi[1];
if (!string.IsNullOrEmpty(aftershi))
{
direction += aftershi.ConvertNameToSmall() * wan;
}
}
else
{
if (!string.IsNullOrEmpty(afterbai))
{
direction += afterbai.ConvertNameToSmall() * wan;
}
}
}
else if (afterqian.Contains("拾"))
{
string beforeshi = "";
string aftershi = "";
string[] arrshi = afterqian.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
beforeshi = arrshi[0];
if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
direction += beforeshi.ConvertNameToSmall() * 10 * wan;
if (!string.IsNullOrEmpty(aftershi))
{
direction += aftershi.ConvertNameToSmall() * wan;
}
}
else
{
if (!string.IsNullOrEmpty(afterqian))
{
direction += afterqian.ConvertNameToSmall() * wan;
}
}
}
else if (content.Contains("佰"))
{
string beforebai = "";
string afterbai = "";
string[] arrbai = content.Split(new string[] { "佰" }, StringSplitOptions.RemoveEmptyEntries);
beforebai = arrbai[0];
if (arrbai.Length == 1) { afterbai = string.Empty; } else { afterbai = arrbai[1]; }
direction += beforebai.ConvertNameToSmall() * 100 * wan;
if (afterbai.Contains("拾"))
{
string beforeshi = "";
string aftershi = "";
string[] arrshi = afterbai.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
beforeshi = arrshi[0];
if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
direction += beforeshi.ConvertNameToSmall() * 10 * wan;
if (!string.IsNullOrEmpty(aftershi))
{
direction += aftershi.ConvertNameToSmall() * wan;
}
}
else
{
if (!string.IsNullOrEmpty(afterbai))
{
direction += afterbai.ConvertNameToSmall() * wan;
}
}
}
else if (content.Contains("拾"))
{
string beforeshi = "";
string aftershi = "";
string[] arrshi = content.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
beforeshi = arrshi[0];
if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
direction += beforeshi.ConvertNameToSmall() * 10 * wan;
if (!string.IsNullOrEmpty(aftershi))
{
direction += aftershi.ConvertNameToSmall() * wan;
}
}
else
{
direction += content.ConvertNameToSmall() * wan;
}
return direction;
}
}
}
C# 金额大写转小写
最新推荐文章于 2023-11-20 18:51:06 发布