数字转换为大写人民币(附源码)_AX

本文介绍使用Visual Studio 2005通过枚举类型和for循环将阿拉伯数字转换为中文数字的方法,并讨论了字符串连接和字符转换过程中的细节。

【咳咳】注意了,这个也是一道笔试题.今天新安装了VS2005,拿它开刀,竟然发现不是想像中那么简单.
【分析】
①用if...else.........是编程的吗?你
②用swtch,我当初就这些写的,常规方法,结果笔试失败!!
③刚刚想到的,用枚举+for
④................还没想到,期待你的发散思维
【思路】
枚举类型学编程的都学过,但感觉不是很常用,以至于我们都忽略了它的存在,今天终于轮到它表现了.
用两个枚举类型,把1,2,3转化为壹,贰,叁     另一个存放{圆,拾,佰.....}
通过for语句判断具体的单位
【处理的问题及总结】
①定义枚举类(下个帖子具体分析一下其中的一个Bug)
②字符串相连  【+=】  具体是怎么连?
string s="AX"; 
    s+="zhz";     //等价于   s=s+"zhz";   而非  s="zhz"+s
基础不扎实啊,..惭愧...
字符转化为数字问题,头一次遇到.
从下图,我们可以清楚的看出:
Convert.ToInt32()不会转化字符,或者说它把字符转化为了ASCII码中对应的号,,,但是在开发环境中提示可以有char类型的参数,因为程序中用到了char类型的参数,才偶然发现这个问题



【源码】

 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Web;
 5None.gifusing System.Web.Security;
 6None.gifusing System.Web.UI;
 7None.gifusing System.Web.UI.WebControls;
 8None.gifusing System.Web.UI.WebControls.WebParts;
 9None.gifusing System.Web.UI.HtmlControls;
10None.gif
11None.gif
12ExpandedBlockStart.gifContractedBlock.gif/**//*只做简单实现,没有对输入进行验证是否合法(非法字符/长度等)*/
13None.gifpublic partial class _Default : System.Web.UI.Page 
14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17ExpandedSubBlockEnd.gif    }

18InBlock.gif    //定义一个枚举类型:数字    
19InBlock.gif    enum Number
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        零,壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖
22ExpandedSubBlockEnd.gif    }

23InBlock.gif    //定义一个枚举类型:单位
24InBlock.gif    enum UnitPrice_AX_Made_In_China
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        圆,拾,佰,仟,万
27ExpandedSubBlockEnd.gif    }

28InBlock.gif    protected void btnSubmit_Click(object sender, EventArgs e)
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30InBlock.gif        //把输入的数字打散(txtNumber是一个文本框,用来输入数字)
31InBlock.gif        char[] n =(this.txtNumber.Text).ToCharArray();
32InBlock.gif        //用来存放显示结果
33InBlock.gif        string show = "";
34InBlock.gif        for (int i = 0; i <n.Length; i++)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            //show+=k  等价于  show=show+k  ,不是我想要的字符串连接
37InBlock.gif            show = ((Number)(Convert.ToInt32(n[n.Length - i-1].ToString()))).ToString()+((UnitPrice_AX_Made_In_China)i).ToString()+show;
38ExpandedSubBlockEnd.gif        }

39InBlock.gif        //lbShow是一个Lable,用来输出结果
40InBlock.gif        this.lbShow.Text=show;
41ExpandedSubBlockEnd.gif    }

42ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值