1using System;
  2using System.Collections;
  3using System.IO;
  4using System.Text;
  5
  6    /**/
  7    /// <summary> 
  8    /// 用于将一个数值型转化为汉语的读法. 
  9    /// </summary> 

 10public class NumberToChn
 11{
 12    public NumberToChn()
 13    {
 14        // 
 15        // TODO: 在此处添加构造函数逻辑 
 16        // 
 17    }

 18
 19    public static string GetChn(decimal dc, bool bUpper)
 20    {
 21        return GetChn(dc.ToString(), bUpper);
 22    }

 23
 24    public static string GetChn(int i, bool bUpper)
 25    {
 26        return GetChn(i.ToString(), bUpper);
 27    }

 28
 29    public static string GetChn(long l, bool bUpper)
 30    {
 31        return GetChn(l.ToString(), bUpper);
 32    }

 33
 34    public static string GetRMBChn(string sDigital, bool bUpper)
 35    {
 36        // 找出第一个点所在的位置,之前的部分用getchn来处理,之后的部分自已处理. 
 37        if (sDigital == null | sDigital.Length == 0)
 38            return "";
 39
 40        int iIndex = sDigital.IndexOf(".");
 41        string sIntPart;
 42        string sDecPart;
 43        if (iIndex == -1)
 44        {
 45            sIntPart = sDigital;
 46            sDecPart = "";
 47        }

 48        else
 49        {
 50            sIntPart = sDigital.Substring(0, iIndex);
 51            sDecPart = sDigital.Substring(iIndex + 1);
 52        }

 53
 54        StringBuilder sb = new StringBuilder(sDigital.Length * 2 + 10);
 55        foreach (char c in sDecPart)
 56        {
 57            if (char.IsDigit(c))
 58            {
 59                sb.Append(c);
 60            }

 61        }

 62        sDecPart = sb.ToString();
 63        sb.Length = 0;
 64
 65        string sTmp = GetChn(sIntPart, bUpper);
 66        if (sTmp != "" && sTmp.Length != 0)
 67        {
 68            sb.Append(sTmp);
 69            sb.Append(bUpper ? '' : '');
 70        }

 71
 72        bool bPrevIsZero = false;
 73        if (sIntPart.Length > 0 && sIntPart.EndsWith("0"&& sb.Length > 1)
 74        {
 75            bPrevIsZero = true;
 76        }

 77        for (int i = 0; i < sDecPart.Length && i < arRMBRight.Length; i++)
 78        {
 79            if (sDecPart[i] == '0')
 80            {
 81                bPrevIsZero = true;
 82            }

 83            else
 84            {
 85                if (bPrevIsZero == true)
 86                {
 87                    sb.Append('');
 88                    bPrevIsZero = false;
 89                }

 90                sb.Append(bUpper ? arDigitals2[sDecPart[i] - '0'] : arDigitals[sDecPart[i] - '0']);
 91                sb.Append(arRMBRight[i]);
 92            }

 93        }

 94        if (sb.Length > 0)
 95        {
 96            sb.Append('');
 97        }

 98        else
 99        {
100            sb.Append(bUpper ? "零圆整" : "零元整");
101        }

102        return sb.ToString();
103    }

104
105    public static string GetChn(string s, bool bUpper)
106    {
107        // 先将s过滤,删除所有的非数字字符。 
108        if (s == null || s.Trim().Length == 0)
109            return "";
110        StringBuilder sb = new StringBuilder(s.Length);
111        int iPointCount = 0;
112        for (int i = 0; i < s.Length; i++)
113        {
114            char c = s[i];
115            if (Char.IsDigit(c))
116            {
117                sb.Append(c);
118            }

119            else if (c == '.')
120            {
121                // 如果是第二个之后的点,那么不管了。 
122                if (iPointCount == 0)
123                {
124                    sb.Append(c);
125                    iPointCount++;
126                }

127            }

128            else if (c == '-' && i == 0)
129            {
130                sb.Append(c);
131            }

132            else
133            {
134                // 剩下的全部忽略 
135            }

136        }

137
138        string sDigital = sb.ToString();
139        if (sDigital.EndsWith("."))
140        {
141            sDigital = sDigital.Substring(0, sDigital.Length - 1);
142        }

143        if (sDigital.Length == 0)
144        {
145            sDigital = "0";
146        }

147
148        sb.Length = 0// 留为后用。 
149
150        // 从小数点前后分为两部分 
151        int iTmp = sDigital.IndexOf('.');
152        string sIntPart;
153        string sDecimalPart;
154        if (iTmp == -1)
155        {
156            sIntPart = sDigital;
157            sDecimalPart = "";
158        }

159        else
160        {
161            sIntPart = sDigital.Substring(0, iTmp);
162            sDecimalPart = sDigital.Substring(iTmp + 1);
163        }

164
165        // 处理小数点之前的部分 
166
167        // 先决定最高位是什么位,再依次地向后拼出。 
168        // 大循环是亿的个数, 小循环是万的个数。 
169
170        sb = new StringBuilder(sDigital.Length * 2 + 6);
171        if (sDigital.StartsWith("-"))
172        {
173            sb.Append("");
174            sIntPart = sIntPart.Substring(1);
175        }

176        // 如果小数点之后没有内容,之前部分又是空,那么不输出. 
177        if (sIntPart.Length == 0 && sDecimalPart.Length == 0)
178        {
179            return "";
180        }

181
182        int iBitCntInWan = 0;
183        bool bPrevIs0 = false;
184        int iWanNotZeroCnt = 0;
185        int iNotZeroCnt = 0;
186
187        for (int i = 0; i < sIntPart.Length; i++)
188        {
189            int iBitCnt = sIntPart.Length - i - 1;
190
191            char cNum = sIntPart[i];
192            if (cNum == '0')
193            {
194                // 如果是零,那么不处理 
195                bPrevIs0 = true;
196            }

197            else
198            {
199                if (bPrevIs0)
200                {
201                    // 如果上一个是0 
202                    if (iNotZeroCnt > 0)
203                    {
204                        //如果不是第一个字 那么加一个零字                                     
205                        sb.Append(bUpper ? arDigitals2[0] : arDigitals[0]);
206                    }

207                }

208                iWanNotZeroCnt++;
209                iNotZeroCnt++;
210
211                bPrevIs0 = false;
212                sb.Append(bUpper ? arDigitals2[cNum - '0'] : arDigitals[cNum - '0']);    // 加数名 
213                iBitCntInWan = iBitCnt % 4;
214                if (iBitCntInWan != 0)
215                {
216                    sb.Append(bUpper ? arRights2[iBitCntInWan] : arRights[iBitCntInWan]);    // 加权名 
217                }

218            }

219
220            if (iBitCnt % 8 == 0)
221            {
222                // 遇亿的处理 
223                if (iBitCnt / 8 >= 1 && iNotZeroCnt > 0)
224                    sb.Append('亿');
225                bPrevIs0 = false;
226                iWanNotZeroCnt = 0;
227            }

228            else if (iBitCnt % 4 == 0)
229            {
230                // 遇万位的处理 
231                if ((iBitCnt % 8/ 4 >= 1 && iWanNotZeroCnt > 0)
232                {
233                    sb.Append('');
234                }

235                bPrevIs0 = false;
236                iWanNotZeroCnt = 0;
237            }

238        }

239
240
241        // 如果全部都是0,那么输出一个"零". 
242        if (iNotZeroCnt == 0)
243        {
244            sb.Append("");
245        }

246
247        // 处理小数点之后的部分 
248        if (sDecimalPart.Length != 0)
249        {
250            sb.Append("");
251            for (int i = 0; i < sDecimalPart.Length; i++)
252            {
253                sb.Append(bUpper ? arDigitals2[sDecimalPart[i] - '0'] : arDigitals[sDecimalPart[i] - '0']);
254            }

255        }

256        iTmp = 0;
257        if (sb[0== '')
258        {
259            iTmp = 1;
260        }

261
262        // 把起头的"一十"改为"拾". 
263        if (sb.Length >= 2)
264        {
265            if (sb[iTmp] == (bUpper ? arDigitals2[1] : arDigitals[1]) &&
266                sb[iTmp + 1== (bUpper ? arRights2[1] : arRights[1]))
267            {
268                sb.Remove(iTmp, 1);
269            }

270        }

271        return sb.ToString();
272    }

273
274    private static char[] arRights = ' ''''''' }// 权名 
275    private static char[] arRights2 = ' ''''''' }// 权名 
276    private static char[] arDigitals = 
277                                           '','','','','','','','','','' 
278                                       }
;
279    private static char[] arDigitals2 = 
280                                            '','','','','','','','','','' 
281                                        }
;
282    private static char[] arRMBRight = '''''''''' };
283
284}