C#对数字添加逗号,千分位

 /// <summary>
/// 对数字添加”,“号,可以处理负数以及带有小数的情况
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
protected string FormatMoney( string money)
{
//处理带有负号情况
int negNumber = money.IndexOf( "-" );
string prefix = string.Empty;
if ( negNumber != - )
{
prefix = "-";
money = money.Substring( );
}
//处理有小数位情况
int decNumber = money.IndexOf( "." );
string postfix = string.Empty;
if ( decNumber != - )
{
postfix = money.Substring( decNumber );
money = money.Substring( , decNumber - );
}
//开始添加”,“号
if ( money.Length > )
{
string str1 = money.Substring( , money.Length - );
string str2 = money.Substring( money.Length - , );
if ( str1.Length > )
{
return prefix + FormatMoney( str1 ) + "," + str2 + postfix;
}
else
{
return prefix + str1 + "," + str2 + postfix;
}
}
else
{
return prefix + money + postfix;
}
}
上一篇:PHP格式化数字和SMARTY格式化数字的方法


下一篇:十五天精通WCF——第三天 client如何知道server提供的功能清单