using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MoneyToChineseUpper
{
/// <summary>
/// 将数字金额转换为大写汉字金额
/// </summary>
public class Converter
{
public static string Convert(doublemoney)
{
string strMoney = money.ToString();
string[] arraryMoney = strMoney.Split('.');
//整数部分
StringBuilder builerInt = newStringBuilder();
if (arraryMoney != null)
{
stringmoneyInt = arraryMoney[0];
int lenOfInt = moneyInt.Length;
int zeroNum = 0;
for (int i = 0; i< lenOfInt; i++)
{
//单位
doubleunit = double.Parse(Math.Pow(10,lenOfInt - 1 - i).ToString());
string strUnit = integerUnitToChineseUpper(unit);
//数值
string strValue = numToChineseUpper(moneyInt[i]);
builerInt.Append(strValue +strUnit);
}
builerInt.Append("整");
}
//小数部份
StringBuilder builerDecimal = new StringBuilder();
if (arraryMoney.Length == 2)
{
builerInt.Remove(builerInt.Length - 1, 1); //移除 "整"
string moneyDecimal = arraryMoney[1];
int lenOfDecimal = moneyDecimal.Length;
for (int i = 0; i< lenOfDecimal; i++)
{
doubleunit = double.Parse(Math.Pow(10,i).ToString());
string strUnit = decimalUnitToChineseUpper(unit);
//数值
string strValue = numToChineseUpper(moneyDecimal[i]);
builerDecimal.Append(strValue +strUnit);
}
}
return builerInt.ToString() +builerDecimal.ToString();
}
/// <summary>
/// 将阿拉伯数字转换为大写汉字
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private static string numToChineseUpper(charnum)
{
string strValue = string.Empty;
switch (num)
{
case '1': strValue= "壹"; break;
case '2': strValue= "貳"; break;
case '3': strValue= "叁"; break;
case '4': strValue= "肆"; break;
case '5': strValue= "伍"; break;
case '6': strValue= "陸"; break;
case'7': strValue = "柒"; break;
case '8': strValue= "捌"; break;
case '9': strValue= "玖"; break;
case '0': strValue= "零"; break;
}
return strValue;
}
/// <summary>
/// 将金额的整数部份转换为汉字单位
/// </summary>
/// <param name="unit"></param>
/// <returns></returns>
private static string integerUnitToChineseUpper(double unit)
{
string strUnit = string.Empty;
if (unit == 1)
strUnit= "元";
else if (unit == 10)
strUnit= "拾";
else if (unit == 100)
strUnit= "佰";
else if (unit ==1000)
strUnit= "仟";
elseif (unit == 10000)
strUnit= "萬";
else if (unit ==100000)
strUnit= "拾";
else if (unit ==1000000)
strUnit= "佰";
else if (unit ==10000000)
strUnit = "仟";
else if (unit ==100000000)
strUnit= "亿";
else if (unit ==1000000000)
strUnit= "拾";
else if (unit ==10000000000)
strUnit= "佰";
else if (unit ==100000000000)
strUnit= "仟";
else
strUnit= "萬";
return strUnit;
}
/// <summary>
/// 将金额的小数部份单位转换为汉字单位
/// </summary>
/// <param name="unit"></param>
/// <returns></returns>
private static string decimalUnitToChineseUpper(double unit)
{
string strUnit = string.Empty;
if (unit == 1)
strUnit= "角";
else if (unit == 10)
strUnit = "分";
else if (unit == 100)
strUnit= "厘";
else if (unit ==1000)
strUnit= "毫厘";
else if (unit ==10000)
strUnit= "微厘";
else
strUnit= "?";
return strUnit;
}
}
}