功能是 能实现, 但是感觉不理想,因为就是对字符串的分割处理,设计的转换,拼接。寻求一个好的方法比如 Linq扩展方法 。把金额数字每三位加个符号。比如123456,123456789, 2345.456,要得到的结果是:123,456  ;  123,456,789  ;    2,345.456 ;实现方法如下:
/// <summary>
        /// 把数字转为要解析的字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string FormatValue(decimal value)
        {
            StringBuilder sb = new StringBuilder();
            string sValue = value.ToString();
            if (!IsHideZeroControl)//是否把不足位数补充0
            {
                if (sValue.Length < Length)
                {
                    int ids = Length - sValue.Length;
                    for (int i = 0; i < ids; i++)
                    {
                        sValue += "0";
                    }
                }
            }            if (IsShowSeparator )
            {
                List<string> list = new List<string>();                string temp = sValue;                bool includeFloat = sValue.LastIndexOf(".") != -1;                if (includeFloat)
                {
                    //temp = temp.Substring(0, sP);
                    temp = ((int)value).ToString();
                }
                int templength = temp.Length;
                if (temp.Length > 3)
                {
                    while (templength > 3)
                    {
                        list.Add(temp.Substring(templength - 3, 3));
                        templength -= 3;
                    }                    //最前面的添加进来
                    list.Add(temp.Substring(0, temp.Length - list.Count * 3));                    for (int i = list.Count - 1; i > 0; i--)
                    {
                        sb.Append(list[i] + ",");
                    }
                    sb.Append(list[0]);                    if (includeFloat)
                    {
                        sb.Append(sValue.Substring(sValue.LastIndexOf(".")));
                    }
                }
                else
                {
                    return sValue;
                }
            }
            return sb.ToString();
        }

解决方案 »

  1.   

    忘记了把产品中的属性给去掉。 单独的方法应该是这样的;
    /// <summary>
            /// 把数字转为要解析的字符串
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            private string FormatValue(decimal value)
            {
                StringBuilder sb = new StringBuilder();            string sValue = value.ToString();            List<string> list = new List<string>();            string temp = sValue;            bool includeFloat = sValue.LastIndexOf(".") != -1;            if (includeFloat)
                {
                    //temp = temp.Substring(0, sP);
                    temp = ((int)value).ToString();
                }            int templength = temp.Length;            if (temp.Length > 3)
                {
                    while (templength > 3)
                    {
                        list.Add(temp.Substring(templength - 3, 3));
                        templength -= 3;
                    }                //最前面的添加进来
                    list.Add(temp.Substring(0, temp.Length - list.Count * 3));                for (int i = list.Count - 1; i > 0; i--)
                    {
                        sb.Append(list[i] + ",");
                    }
                    sb.Append(list[0]);                if (includeFloat)
                    {
                        sb.Append(sValue.Substring(sValue.LastIndexOf(".")));
                    }
                }
                else
                {
                    return sValue;
                }
                return sb.ToString();
            }
      

  2.   

    如果楼主只为显示数字的话,可以试试这个方法
    double d = 1234567.123333;
    Console.WriteLine(d.ToString("N2"));//N2中的2为保留2位小数,如果要保留三位可写为N3
      

  3.   

     public string FormatValue(decimal value)
                {
                    string tempStr = value.ToString();
                    Match m = Regex.Match(tempStr,@"(\d+)(\.\d+)?");
                    string before_str = m.Groups[1].Value;
                    string after_str = m.Groups[2].Value;
                    before_str = string.Join("",before_str.Reverse());
                    before_str = Regex.Replace(before_str,@"(?<=\G\d{3})(?=(?!$).)",",");
                    before_str = string.Join("", before_str.Reverse());
                    return before_str+after_str;
                }不过就本国国情来讲,那么多小数貌似没什么意义
      

  4.   

    NumberFormatInfo应该可以解决你的问题。
    数值类型的ToString方法有重载的带参的方法:
    ToString()                      将此实例的数值转换为其等效的字符串表示形式。 (重写ValueType.ToString()。)
    ToString(IFormatProvider)      使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。
    ToString(String)              使用指定的格式,将此实例的数值转换为它的等效字符串表示形式。
    ToString(String, IFormatProvider)   使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。 
      

  5.   

    +1;这个还弄的这么纠结....string.Format()和tostring()都可以
      

  6.   


    private string FormatV(decimal value)
            {
                StringBuilder sb = new StringBuilder(string.Empty);            string sValue = value.ToString();            string temp = sValue;            bool includeFloat = sValue.LastIndexOf(".") != -1;            if (includeFloat)
                {
                    //temp = temp.Substring(0, sP);
                    temp = ((int)value).ToString();
                }            if (temp.Length > 3)
                {
                    int len = sValue.Length;
                    int leftnum = len % 3;                for (int i = 0; i < len; i++)
                    {
                        sb.Append(sValue[i]);
                        if ((len - i - (leftnum - 1) - 1) % 3 == 0 && (len - i - (leftnum - 1) - 1) != 0)
                        {
                            sb.Append(',');
                        }
                    }
                    if (includeFloat)
                    {
                        sb.Append(sValue.Substring(sValue.LastIndexOf(".")));
                    }
                }
                else
                {
                    return sValue;
                }            return sb.ToString();
            }
      

  7.   


    这样写方便点,不过小数位数怎么办? 不还是要截取小数点后面字符串 个数。
                     int n = sValue.Substring(sValue.LastIndexOf(".") + 1).Length;                string ccc = value.ToString("C" + n.ToString());
      

  8.   


    private string FormatValue(decimal value)
            {
                StringBuilder sb = new StringBuilder(string.Empty);            string sValue = value.ToString();            string temp = sValue;            var sdf = sValue.Split('.');            if (sdf.Length > 1)
                {
                    temp = sdf[0];
                }            if (temp.Length > 3)
                {
                    int len = temp.Length;
                    int startPosition = 4;                for (int i = 0; i < len; i++)
                    {
                        sb.Append(temp[i]);                    int n = len - i;
                        int condition = n - startPosition;                    if (condition % 3 == 0 && condition >= 0)
                        {
                            sb.Append(',');
                        }
                    }                if (sdf.Length > 1)
                    {
                        sb.Append('.').Append(sdf[1]);
                    }
                }
                else
                {
                    return sValue;
                }            return sb.ToString();
            }
      

  9.   


    private string FormatV(decimal value)
            {
                StringBuilder sb = new StringBuilder(string.Empty);            string sValue = value.ToString();            string temp = sValue;            var sdf = sValue.Split('.');            if (sdf.Length > 1)
                {
                    temp = sdf[0];
                }            if (temp.Length > 3)
                {
                    int len = temp.Length;                int startPosition = len % 3 == 0 ? 2 : len % 3 - 1;                int j = 0;                for (int i = 0; i < len; i++)
                    {
                        sb.Append(temp[i]);                    if (i == (startPosition + j * 3) && i != len - 1)
                        {
                            sb.Append(',');
                            j++;
                        }
                    }                if (sdf.Length > 1)
                    {
                        sb.Append('.').Append(sdf[1]);
                    }
                }
                else
                {
                    return sValue;
                }            return sb.ToString();
            }改成这样好理解点,而且似乎更好一些
      

  10.   

    簡單的功能不需要用太多複雜的手段...
    下面是我的做法,而且不受位數影響^_^private string formatThousnadSymaol(string SrcNum)
    {
        string flt = SrcNum.IndexOf('.') < 0 ? "" : SrcNum.Split('.')[1];
        SrcNum = SrcNum.Split('.')[0];
        int pnt = SrcNum.Length % 3;
        if (SrcNum.Length > 3)
        {
            while (pnt + 1 < SrcNum.Length)
            {
                int i = pnt % 4 == 0 ? pnt : pnt + 1;
                if (i > 0) SrcNum = SrcNum.Insert(i - 1, ",");
                pnt += 4;
            }
        }
        return SrcNum + flt;
    }