格式化为中文数字

namespace Microshaoft
{
    
using System;

    
public class ChineseFormat : System.ICustomFormatter, System.IFormatProvider
    
{
        
//如果format Type与当前实例类型相同,则为当前实例,否则为空引用 
        public object GetFormat(Type format)
        
{
            
if (format == typeof (ICustomFormatter))
            
{
                
return this;
            }

            
return null;
        }


        
//实现Format方法说明: 
        
//如果您的格式方法不支持格式,则确定正在设置格式的对象是否实现 IFormattable 接口。 
        
//如果实现,请调用该接口的IFormattable.ToString 方法。 
        
//否则,调用基础对象的默认 Object.ToString 方法。 
        public string Format(string format, object arg, IFormatProvider provider)
        
{
            
if (format == null)
            
{
                
if (arg is IFormattable)
                
{
                    
return ((IFormattable) arg).ToString(format, provider);
                }

                
return arg.ToString();
            }

            
else
            
{
                
if (format == "ChineseFormat")
                
{
                    
string[] Nums = new string[] {""""""""""""""""""""};
                    
//位 数组 
                    string[] Digits = new string[] {""""""""};
                    
//单位 数组 
                    string[] Units = new string[] {"""[万]""[亿]""[万亿]"};
                    
return ConvertNumberToChinese(arg.ToString(), Nums, Digits, Units);
                    
//return "***"+arg.ToString(); 
                }

                
else
                
{
                    
if (arg is IFormattable)
                    
{
                        
return ((IFormattable) arg).ToString(format, provider);
                    }

                    
return arg.ToString();
                }

            }

        }


        
public static string ConvertNumberToChinese(string x, string[] Nums, string[] Digits, string[] Units)
        
{
            
string S = ""//返回值 
            int p = 0//字符位置指针 
            int m = x.Length % 4//取模 

            
// 四位一组得到组数 
            int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);

            
// 外层循环在所有组中循环 
            
// 从左到右 高位到低位 四位一组 逐组处理 
            
// 每组最后加上一个单位: "[万亿]","[亿]","[万]" 
            for (int i = k; i > 0; i--)
            
{
                
int L = 4;
                
if (i == k && m != 0)
                
{
                    L 
= m;
                }

                
// 得到一组四位数 最高位组有可能不足四位 
                string s = x.Substring(p, L);
                
int l = s.Length;

                
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位 
                for (int j = 0; j < l; j++)
                
{
                    
//处理改组中的每一位数加上所在位: "仟","佰","拾",""(个) 
                    int n = Convert.ToInt32(s.Substring(j, 1));
                    
if (n == 0)
                    
{
                        
if (j < l - 1
                            
&& Convert.ToInt32(s.Substring(j + 11)) > 0 //后一位(右低) 
                            && !S.EndsWith(Nums[n]))
                        
{
                            S 
+= Nums[n];
                        }

                    }

                    
else
                    
{
                        
//处理 1013 一千零"十三", 1113 一千一百"一十三" 
                        if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0&& j == l - 2))
                        
{
                            S 
+= Nums[n];
                        }

                        S 
+= Digits[l - j - 1];
                    }

                }

                p 
+= L;
                
// 每组最后加上一个单位: [万],[亿] 等 
                if (i < k) //不是最高位的一组 
                {
                    
if (Convert.ToInt32(s) != 0)
                    
{
                        
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等 
                        S += Units[i - 1];
                    }

                }

                
else
                
{
                    
//处理最高位的一组,最后必须加上单位 
                    S += Units[i - 1];
                }

            }

            
return S;
        }

    }

}


namespace Test
{
    
using System;
    
using Microshaoft;

    
class AppTest
    
{
        
static void Main()
        
{
            
string printString = String.Empty;
            
long i = 1100000013;
            ChineseFormat fmt 
= new ChineseFormat();

            printString 
= string.Format(fmt, "显示正常格式: {0}", i);
            Console.WriteLine(printString);
            printString 
= string.Format(fmt, "显示正常格式: {0:C}", i);
            Console.WriteLine(printString);
            printString 
= string.Format(fmt, "显示自定义格式: {0:ChineseFormat}", i);
            Console.WriteLine(printString);

            Console.ReadLine();
        }

    }

}


posted on 2009-03-09 15:08  冬日阳光  阅读(277)  评论(0编辑  收藏  举报

导航