格式化数字,千分位符,有效数字,汉字显示

  public static class NumberToFormat
    {
        /// <summary>
        /// 数字转换为汉字显示
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string NumberToChinese(int number)
        {
            string[] UNITS = { "", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
            string[] NUMS = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            if (number == 0)
            {
                return NUMS[0];
            }
            string results = "";
            for (int i = number.ToString().Length - 1; i >= 0; i--)
            {
                int r = (int)(number / (Math.Pow(10, i)));
                results += NUMS[r % 10] + UNITS[i];
            }
            results = results.Replace("零十", "零")
                                .Replace("零百", "零")
                                .Replace("零千", "零")
                                .Replace("亿万", "亿");
            results = Regex.Replace(results, "零([万, 亿])", "$1");
            results = Regex.Replace(results, "零+", "零");

            if (results.StartsWith("一十"))
            {
                results = results.Substring(1);
            }
        cutzero:
            if (results.EndsWith("零"))
            {
                results = results.Substring(0, results.Length - 1);
                if (results.EndsWith("零"))
                {
                    goto cutzero;
                }
            }
            return results;

        }


        /// <summary>
        /// 转换为千分位符
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string NumToN(decimal number)
        {
            //转换为字符串
            string numstr = number.ToString();


            if (numstr == "0")
            {
                numstr = "0";
            }


            else if (numstr.Contains("."))
            {
                //获取整数部位
                decimal intpart = Convert.ToDecimal(numstr.Substring(0, numstr.IndexOf(".")));
                if (intpart == 0)
                {
           
                }
                else
                {
       
                    numstr = string.Format("{0}{1}", intpart.ToString("###,###"), numstr.Substring(numstr.IndexOf(".")));
                }
            }

            else
            {
                //0会变为""
                numstr = number.ToString("###,###");
            }
          
            return numstr;
        }


        /// <summary>
        /// 转换为有效数字
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static decimal NumToG(decimal number)
        {
            //转换为字符串
            string numstr = number.ToString("G0");
            return Convert.ToDecimal(numstr);
        }

 

上一篇:时间序列模型梳理


下一篇:Lianjia Analysis Project