//// <summary>
/// 人民币大小写金额转换
/// </summary>
class RMBCapitalization
{
private const string DXSZ = "零壹贰叁肆伍陆柒捌玖";
private const string DXDW = "毫厘分角元拾佰仟萬拾佰仟亿拾佰仟萬兆拾佰仟萬亿京拾佰仟萬亿兆垓";
private const string SCDW = "元拾佰仟萬亿京兆垓"; /// <summary>
/// 转换整数为大写金额
/// 最高精度为垓,保留小数点后4位,实际精度为亿兆已经足够了,理论上精度无限制,如下所示:
/// 序号:...30.29.28.27.26.25.24 23.22.21.20.19.18 17.16.15.14.13 12.11.10.9 8 7.6.5.4 . 3.2.1.0
/// 单位:...垓兆亿萬仟佰拾 京亿萬仟佰拾 兆萬仟佰拾 亿仟佰拾 萬仟佰拾元 . 角分厘毫
/// 数值:...1000000 000000 00000 0000 00000 . 0000
/// 下面列出网上搜索到的数词单位:
/// 元、十、百、千、万、亿、兆、京、垓、秭、穰、沟、涧、正、载、极
/// </summary>
/// <param name="capValue">整数值</param>
/// <returns>返回大写金额</returns>
private static string ConvertIntToUppercaseAmount(string capValue)
{
string currCap = ""; //当前金额
string capResult = ""; //结果金额
string currentUnit = "";//当前单位
string resultUnit = ""; //结果单位
int prevChar = -; //上一位的值
int currChar = ; //当前位的值
int posIndex = ; //位置索引,从"元"开始 if (Convert.ToDouble(capValue) == ) return "";
for (int i = capValue.Length - ; i >= ; i--)
{
currChar = Convert.ToInt16(capValue.Substring(i, ));
if (posIndex > )
{
//已超出最大精度"垓"。注:可以将30改成22,使之精确到兆亿就足够了
break;
}
else if (currChar != )
{
//当前位为非零值,则直接转换成大写金额
currCap = DXSZ.Substring(currChar, ) + DXDW.Substring(posIndex, );
}
else
{
//防止转换后出现多余的零,例如:3000020
switch (posIndex)
{
case : currCap = "元"; break;
case : currCap = "萬"; break;
case : currCap = "亿"; break;
case : currCap = "兆"; break;
case : currCap = "京"; break;
case : currCap = "垓"; break;
default: break;
}
if (prevChar != )
{
if (currCap != "")
{
if (currCap != "元") currCap += "零";
}
else
{
currCap = "零";
}
}
}
//对结果进行容错处理
if (capResult.Length > )
{
resultUnit = capResult.Substring(, );
currentUnit = DXDW.Substring(posIndex, );
if (SCDW.IndexOf(resultUnit) > )
{
if (SCDW.IndexOf(currentUnit) > SCDW.IndexOf(resultUnit))
{
capResult = capResult.Substring();
}
}
}
capResult = currCap + capResult;
prevChar = currChar;
posIndex += ;
currCap = "";
}
return capResult;
} /// <summary>
/// 转换小数为大写金额
/// </summary>
/// <param name="capValue">小数值</param>
/// <param name="addZero">是否增加零位</param>
/// <returns>返回大写金额</returns>
private static string ConvertDecToUppercaseAmount(string capValue, bool addZero)
{
string currCap = "";
string capResult = "";
int prevChar = addZero ? - : ;
int currChar = ;
int posIndex = ; if (Convert.ToInt16(capValue) == ) return "";
for (int i = ; i < capValue.Length; i++)
{
currChar = Convert.ToInt16(capValue.Substring(i, ));
if (currChar != )
{
currCap = DXSZ.Substring(currChar, ) + DXDW.Substring(posIndex, );
}
else
{
if (Convert.ToInt16(capValue.Substring(i)) == )
{
break;
}
else if (prevChar != )
{
currCap = "零";
}
}
capResult += currCap;
prevChar = currChar;
posIndex -= ;
currCap = "";
}
return capResult;
} /// <summary>
/// 人民币大写金额
/// </summary>
/// <param name="value">人民币数字金额值</param>
/// <returns>返回人民币大写金额</returns>
public static string RMBAmount(double value)
{
string capResult = "";
string capValue = string.Format("{0:f4}", value); //格式化
int dotPos = capValue.IndexOf("."); //小数点位置
bool addInt = (Convert.ToInt32(capValue.Substring(dotPos + )) == );//是否在结果中加"整"
bool addMinus = (capValue.Substring(, ) == "-"); //是否在结果中加"负"
int beginPos = addMinus ? : ; //开始位置
string capInt = capValue.Substring(beginPos, dotPos); //整数
string capDec = capValue.Substring(dotPos + ); //小数 if (dotPos > )
{
capResult = ConvertIntToUppercaseAmount(capInt) +
ConvertDecToUppercaseAmount(capDec, Convert.ToDouble(capInt) != ? true : false);
}
else
{
capResult = ConvertIntToUppercaseAmount(capDec);
}
if (addMinus) capResult = "负" + capResult;
if (addInt) capResult += "整";
return capResult;
}
}

此类方法实现了将人民币数字金额转换为小写,直接调用RMBCapitalization.RMBAmount(人民币数字金额)

05-04 07:54