C#:转换成中文数字

       

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
/// <summary>
/// 转换成中文数字
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string ToChineseNum( double number)
{
string numList = " 零壹贰叁肆伍陆柒捌玖 " ;
string rmbList = " 分角元拾佰仟万拾佰仟亿拾佰仟万 " ;
string tempOutString = null ;


if (number < 0 )
{
number
= - number;
tempOutString
= " " ;
}

if (number > 9999999999999.99 )
return " 超出范围的人民币值 " ;

// 将小数转化为整数字符串
string tempNumberString = Convert.ToInt64(number * 100 ).ToString();
int tempNmberLength = tempNumberString.Length;
int i = 0 ;
while (i < tempNmberLength)
{
int oneNumber = Int32.Parse(tempNumberString.Substring(i, 1 ));
string oneNumberChar = numList.Substring(oneNumber, 1 );
string oneNumberUnit = rmbList.Substring(tempNmberLength - i - 1 , 1 );
if (oneNumberChar != " " )
tempOutString
+= oneNumberChar + oneNumberUnit;
else
{
if (oneNumberUnit == " 亿 " || oneNumberUnit == " " || oneNumberUnit == " " || oneNumberUnit == " " )
{
while (tempOutString.EndsWith( " " ))
{
tempOutString
= tempOutString.Substring( 0 , tempOutString.Length - 1 );
}
}
if (oneNumberUnit == " 亿 " || (oneNumberUnit == " " && ! tempOutString.EndsWith( " 亿 " )) || oneNumberUnit == " " )
{
tempOutString
+= oneNumberUnit;
}
else
{
try
{
bool tempEnd = tempOutString.EndsWith( " 亿 " );
bool zeroEnd = tempOutString.EndsWith( " " );
if (tempOutString.Length > 1 )
{
bool zeroStart = tempOutString.Substring(tempOutString.Length - 2 , 2 ).StartsWith( " " );
if ( ! zeroEnd && (zeroStart || ! tempEnd))
tempOutString
+= oneNumberChar;
}
else
{
if ( ! zeroEnd && ! tempEnd)
tempOutString
+= oneNumberChar;
}
}
catch
{
return " 零元零分 " ;
}
}
}
i
+= 1 ;
}

while (tempOutString.EndsWith( " " ))
{
tempOutString
= tempOutString.Substring( 0 , tempOutString.Length - 1 );
}

if (tempOutString.EndsWith( " " ) || tempOutString.EndsWith( " " ))
{
tempOutString
= tempOutString + " " ;
}

return tempOutString;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值