C#获取千分位,给数字加逗号分隔符

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

 

posted @ 2015-12-21 22:45  凌晨10点13分  阅读(1430)  评论(0编辑  收藏  举报