惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - sashow

c++知识点--extern "C"的作用 - sashow - 博客园 粘贴一点有用的东西留存-- Control 类的事件顺序 开始→运行→输入命令集锦 c#的ThreadPool使用笔记(四)--实例:端口扫描程序 c#的ThreadPool使用笔记(三) c# 中Label 标签和.resx 文件中的换行问题 - sashow 计算某天是星期几的算法 find 和 grep 命令 - sashow Sql Server 中由数字转换为指定长度的字符串 [分享] C# 中 Socket 进行数据接收的一点心得 sql server 2005 使用问题总结 [转]解决无法显示所有文件和文件夹,无法显示隐藏文件和文件夹 FileStream进行Read() 操作后文件指针指向当前位置 用回车键模拟TAB键的方法 c#的ThreadPool使用笔记(二) c#的ThreadPool使用笔记(一) 放弃还是继续? 随便写点东西 [转贴]Informix SQL函数的详细用法
人民币大小写转换(C#)
sashow · 2006-12-04 · via 博客园 - sashow

    今天写了一个C#版本的大小写转换类,想着也要更新一下我的日记了,便把它传了上来。 哪位路过的高手,请指点。
    这个类提供了一个静态方法叫做 Convert ( double ),传入一个9千万亿以内的数值它都能正确的转换为中文的大写。基本的思路是将传入的数据分为整数部分和小数部分分别来处理,小数部分的处理比较简单,整数部分的处理我作了相应的注释。

  1using System;
  2
  3namespace Tools{
  4    /// <summary>
  5    /// DigitalToChinese :实现将人民币的数字形式转换为中文格式。
  6    /// </summary>

  7    public class DigitalToChinese
  8    {
  9        public DigitalToChinese(){
 10        }

 11
 12        public static string Convert ( double digital ){
 13            if ( digital == 0.00f )
 14                return "零元整";
 15
 16            string buf = "";                        /* 存放返回结果 */
 17            string strDecPart = "";                    /* 存放小数部分的处理结果 */
 18            string strIntPart = "";                    /* 存放整数部分的处理结果 */
 19
 20            /* 将数据分为整数和小数部分 */
 21            char [] cDelim = {'.'};
 22            string [] tmp = null;
 23            string strDigital = digital.ToString ();
 24            if ( strDigital[0=='.'){                /* 处理数据首位为小数点的情况 */
 25                strDigital = "0" + strDigital;
 26            }

 27            tmp = strDigital.Split ( cDelim,2);
 28
 29            /* 整数部分的处理 */
 30            if ( tmp[0].Length > 15 ) {
 31                throw new Exception ("数值超出处理范围。");
 32            }

 33            bool flag = true;        /* 标示整数部分是否为零 */
 34            if ( digital >= 1.00f ) {
 35                strIntPart = ConvertInt( tmp[0] );
 36                flag = false;
 37            }

 38
 39            /* 存在小数部分,则处理小数部分 */
 40            if ( tmp.Length == 2 )    
 41                strDecPart = ConvertDecimal ( tmp[1],flag);
 42            else
 43                strDecPart = "";
 44
 45            buf = strIntPart + strDecPart;
 46            return buf;
 47        }

 48
 49
 50
 51        private static string ConvertInt ( string intPart ) {
 52            string buf = "";
 53            int length = intPart.Length ;
 54            int curUnit = length;
 55
 56            /* 处理除个位以上的数据 */
 57            string tmpValue = "";                    /* 记录当前数值的中文形式 */
 58            string tmpUnit = "";                    /* 记录当前数值对应的中文单位 */
 59            int i ;
 60            for ( i = 0 ; i < length - 1 ; i ++ ,curUnit --){
 61                if ( intPart [ i ] != '0' ){
 62                    tmpValue = DigToCC ( intPart [ i ] );
 63                    tmpUnit = GetUnit ( curUnit -1 );
 64                }

 65                else {
 66                    /* 如果当前的单位是"万、亿",则需要把它记录下来 */
 67                    if ( (curUnit -1 ) % 4 == 0 ) {
 68                        tmpValue = "";
 69                        tmpUnit = GetUnit ( curUnit -1 );
 70                    }

 71                    else {
 72                        tmpUnit = "";
 73                        /* 如果当前位是零,则需要判断它的下一位是否为零,再确定是否记录'零' */
 74                        if (   intPart [i+1!= '0' ){
 75                            tmpValue = "";
 76                        }

 77                        else {
 78                            tmpValue = "";
 79                        }

 80                    }

 81                }

 82                buf += tmpValue + tmpUnit;
 83            }

 84
 85            /* 处理个位数据 */
 86            if ( intPart [i] != '0' )
 87                buf += DigToCC ( intPart [i] );
 88            buf += "";
 89
 90            return buf;
 91        }

 92
 93
 94        /// <summary>
 95        /// 小数部分的处理
 96        /// </summary>
 97        /// <param name="decPart">需要处理的小数部分</param>
 98        /// <param name="flag">标示整数部分是否为零</param>
 99        /// <returns></returns>

100        private static string ConvertDecimal ( string decPart,bool flag ) {
101            string buf = "";
102            if ( ( decPart [0== '0'&& (decPart.Length  > 1 )){
103                if ( flag == false ) {
104                    buf += "";
105                }

106                if ( decPart[1!= '0'){
107                    buf += DigToCC( decPart[1] )+"";
108                }

109            }

110            else{
111                buf += DigToCC ( decPart[0] ) +"";
112                if ( ( decPart.Length >1&& (decPart [1!= '0' ) ){
113                    buf += DigToCC( decPart[1] )+"";
114                }

115            }

116            buf += "";
117            return buf;
118        }

119
120
121        /// <summary>
122        /// 获取人民币中文形式的对应位置的单位标志
123        /// </summary>
124        /// <param name="n"></param>
125        /// <returns></returns>

126        private static string GetUnit( int n ) {
127            switch( n ) {
128                case 1:
129                    return "";
130                case 2:
131                    return "";
132                case 3:
133                    return "";
134                case 4:
135                    return "";
136                case 5:
137                    return "";
138                case 6:
139                    return "";
140                case 7:
141                    return "";
142                case 8:
143                    return "亿";
144                case 9:
145                    return "";
146                case 10:
147                    return "";
148                case 11:
149                    return "";
150                case 12:
151                    return "";
152                case 13:
153                    return "";
154                case 14:
155                    return "";
156                case 15:
157                    return "";
158                default:
159                    return " ";
160            }

161        }

162
163
164
165        /// <summary>
166        /// 数字转换为相应的中文字符 ( Digital To Chinese Char )
167        /// </summary>
168        /// <param name="c">以字符形式存储的数字</param>
169        /// <returns></returns>

170        private static string DigToCC(char c) {
171            switch( c ) {
172                case '1':
173                    return "";
174                case '2':
175                    return "";
176                case '3':
177                    return "";
178                case '4':    
179                    return "";
180                case '5':
181                    return "";
182                case '6':
183                    return "";
184                case '7':
185                    return "";
186                case '8':
187                    return "";
188                case '9':
189                    return "";
190                case '0':
191                    return "";
192                default:
193                    return "  ";
194            }

195        }

196    }

197}

198

下面是一个测试代码:

 1using System;
 2
 3namespace Tools{
 4    /// <summary>
 5    /// Class1 的摘要说明。
 6    /// </summary>

 7    class Class1
 8    {
 9        /// <summary>
10        /// 应用程序的主入口点。
11        /// </summary>

12        [STAThread]
13        static void Main(string[] args)
14        {
15            string read = null;
16            Console.WriteLine ("输入数值,'q'推出!");
17
18            do{
19                
20                read = Console.ReadLine ();
21                if ( read =="q" || read == "Q")
22                    return;
23                try{
24                    Console.WriteLine ( DigitalToChinese.Convert ( Convert.ToDouble ( read )));
25                }

26                catch ( Exception  ){
27                    Console.WriteLine ("输入的数据不符合要求,请输入正确的数值.");
28                }

29            }
while ( 1 == 1 );
30        }

31    }

32}

33