人民币大写转换
internal class RMBUppercaseHelper
{
private const string CN_ZERO = "零";
private const string CN_DOLLAR = "元";
private const string CN_INTEGER = "整";
private const string REG_NUMBER_COMMA_POINT = @"^[0-9,.]+$";
private const string REG_CURRENCY = @"^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$";
private static readonly string[] chineseDigits = new string[] { CN_ZERO, "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private static readonly string[] chineseSmallUnits = new string[] { string.Empty, "拾", "佰", "仟" };
private static readonly string[] chineseBigUnits = new string[] { string.Empty, "万", "亿", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载" };
private static readonly string[] chineseDecimalsUnits = new string[] { "角", "分", "厘", "毫", "丝", "忽", "微" };
public static string Converter(string money, out string message)
{
message = string.Empty;
if (string.IsNullOrWhiteSpace(money))
{
message = "金额不能为空";
return string.Empty;
}
if (!Regex.IsMatch(money, REG_NUMBER_COMMA_POINT))
{
message = "金额中存在无效字符";
return string.Empty;
}
if (!Regex.IsMatch(money, REG_CURRENCY))
{
message = "金额字符串格式错误";
return string.Empty;
}
money = Regex.Replace(money, @",", string.Empty);
money = Regex.Replace(money, @"^0+", string.Empty);
decimal inputMoney = 0;
if (!decimal.TryParse(money, out inputMoney))
{
message = "不是有效的金额";
return string.Empty;
}
string integerPart;
string fractionPart;
if (!SplitMoney(inputMoney, out integerPart, out fractionPart))
{
message = $"金额拆分错误";
return string.Empty;
}
string outputCharacters = "";
if (!string.IsNullOrEmpty(integerPart) && long.Parse(integerPart) > 0)
{
int zeroCount = 0;
for (int i = 0; i < integerPart.Length; i++)
{
int p = integerPart.Length - i - 1;
int indexNumber = int.Parse(integerPart.Substring(i, 1));
int quotient = p / 4;
int modulus = p % 4;
if (indexNumber == 0)
{
zeroCount++;
}
else
{
if (zeroCount > 0)
{
outputCharacters += chineseDigits[0];
}
zeroCount = 0;
outputCharacters += chineseDigits[indexNumber] + chineseSmallUnits[modulus];
}
if (modulus == 0 && zeroCount < 4)
{
outputCharacters += chineseBigUnits[quotient];
}
}
outputCharacters += CN_DOLLAR;
}
if (!string.IsNullOrEmpty(fractionPart))
{
for (int i = 0; i < fractionPart.Length; i++)
{
int indexNumber = int.Parse(fractionPart.Substring(i, 1));
if (indexNumber != 0)
{
outputCharacters += chineseDigits[indexNumber] + chineseDecimalsUnits[i];
}
}
}
if (outputCharacters == "")
{
outputCharacters = CN_ZERO + CN_DOLLAR;
}
if (string.IsNullOrEmpty(fractionPart))
{
outputCharacters += CN_INTEGER;
}
return outputCharacters;
}
private static bool SplitMoney(decimal value, out string integerPart, out string fractionPart)
{
integerPart = string.Empty;
fractionPart = string.Empty;
try
{
string numberStr = value.ToString(CultureInfo.InvariantCulture);
int decimalIndex = numberStr.IndexOf('.');
if (decimalIndex > 0)
{
integerPart = numberStr.Substring(0, decimalIndex);
fractionPart = numberStr.Substring(decimalIndex + 1);
}
else
{
integerPart = value.ToString();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
}