将一个数值型转化为汉语的读法

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

 10 None.gif public   class  NumberToChn
 11 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 12InBlock.gif    public NumberToChn()
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14InBlock.gif        // 
 15InBlock.gif        // TODO: 在此处添加构造函数逻辑 
 16InBlock.gif        // 
 17ExpandedSubBlockEnd.gif    }

 18InBlock.gif
 19InBlock.gif    public static string GetChn(decimal dc, bool bUpper)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21InBlock.gif        return GetChn(dc.ToString(), bUpper);
 22ExpandedSubBlockEnd.gif    }

 23InBlock.gif
 24InBlock.gif    public static string GetChn(int i, bool bUpper)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 26InBlock.gif        return GetChn(i.ToString(), bUpper);
 27ExpandedSubBlockEnd.gif    }

 28InBlock.gif
 29InBlock.gif    public static string GetChn(long l, bool bUpper)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 31InBlock.gif        return GetChn(l.ToString(), bUpper);
 32ExpandedSubBlockEnd.gif    }

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

 48InBlock.gif        else
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            sIntPart = sDigital.Substring(0, iIndex);
 51InBlock.gif            sDecPart = sDigital.Substring(iIndex + 1);
 52ExpandedSubBlockEnd.gif        }

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

 61ExpandedSubBlockEnd.gif        }

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

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

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

 83InBlock.gif            else
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 85InBlock.gif                if (bPrevIsZero == true)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 87InBlock.gif                    sb.Append('');
 88InBlock.gif                    bPrevIsZero = false;
 89ExpandedSubBlockEnd.gif                }

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

 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif        if (sb.Length > 0)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            sb.Append('');
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif        else
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            sb.Append(bUpper ? "零圆整" : "零元整");
101ExpandedSubBlockEnd.gif        }

102InBlock.gif        return sb.ToString();
103ExpandedSubBlockEnd.gif    }

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

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

127ExpandedSubBlockEnd.gif            }

128InBlock.gif            else if (c == '-' && i == 0)
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                sb.Append(c);
131ExpandedSubBlockEnd.gif            }

132InBlock.gif            else
133ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
134InBlock.gif                // 剩下的全部忽略 
135ExpandedSubBlockEnd.gif            }

136ExpandedSubBlockEnd.gif        }

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

143InBlock.gif        if (sDigital.Length == 0)
144ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
145InBlock.gif            sDigital = "0";
146ExpandedSubBlockEnd.gif        }

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

159InBlock.gif        else
160ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
161InBlock.gif            sIntPart = sDigital.Substring(0, iTmp);
162InBlock.gif            sDecimalPart = sDigital.Substring(iTmp + 1);
163ExpandedSubBlockEnd.gif        }

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

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

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

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

207ExpandedSubBlockEnd.gif                }

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

218ExpandedSubBlockEnd.gif            }

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

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

235InBlock.gif                bPrevIs0 = false;
236InBlock.gif                iWanNotZeroCnt = 0;
237ExpandedSubBlockEnd.gif            }

238ExpandedSubBlockEnd.gif        }

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

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

255ExpandedSubBlockEnd.gif        }

256InBlock.gif        iTmp = 0;
257InBlock.gif        if (sb[0== '')
258ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
259InBlock.gif            iTmp = 1;
260ExpandedSubBlockEnd.gif        }

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

270ExpandedSubBlockEnd.gif        }

271InBlock.gif        return sb.ToString();
272ExpandedSubBlockEnd.gif    }

273InBlock.gif
274ExpandedSubBlockStart.gifContractedSubBlock.gif    private static char[] arRights = dot.gif' ''''''' }// 权名 
275ExpandedSubBlockStart.gifContractedSubBlock.gif    private static char[] arRights2 = dot.gif' ''''''' }// 权名 
276ExpandedSubBlockStart.gifContractedSubBlock.gif    private static char[] arDigitals = dot.gif
277InBlock.gif                                           '','','','','','','','','','' 
278ExpandedSubBlockEnd.gif                                       }
;
279ExpandedSubBlockStart.gifContractedSubBlock.gif    private static char[] arDigitals2 = dot.gif
280InBlock.gif                                            '','','','','','','','','','' 
281ExpandedSubBlockEnd.gif                                        }
;
282ExpandedSubBlockStart.gifContractedSubBlock.gif    private static char[] arRMBRight = dot.gif'''''''''' };
283InBlock.gif
284ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Jo/archive/2005/07/26/200389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值