C#,JAVA人民币小写转大写

在CSDN里看到的,先在这里copy到这里留作以后备用

1

View Code
1 package fendou.wyb.io;
2
3  public class MoneySmallToBig {
4
5 /**
6 * 人民币大小写转换
7 */
8 public static String[] number = new String[]{"","","","","","","","","","" };
9 public static String[] units = new String[]{"","","",""};
10 public static void change(String smoney){
11 StringBuffer sb=new StringBuffer();
12 //小数点的位置
13 int index=smoney.indexOf(".");
14
15 //biggerstr用于存放存有数字和单位的字符
16 StringBuffer biggerstr=new StringBuffer();
17
18 //处理小数部分
19 biggerstr.append("");
20 biggerstr.append(number[Integer.parseInt(smoney.charAt(index+2)+"")]);
21 biggerstr.append("");
22 biggerstr.append(number[Integer.parseInt(smoney.charAt(index+1)+"")]);
23
24 //处理整数部分
25 for(int i=0;i<index;i++){
26 sb.append(number[Integer.parseInt(smoney.charAt(i)+"")]);
27 }
28 sb=sb.reverse();
29 String insertstr=sb.toString();
30 for(int i=0;i<insertstr.length();i++){
31 //插入单位
32 if(i==0){
33 biggerstr.append("");
34 }
35 else if((i+4)%4==0 && i!=8){
36 biggerstr.append("");
37 }
38 else if(i%8==0){
39 biggerstr.append("亿");
40 }
41 else
42 biggerstr.append(units[i%4]);
43 //插入数字
44 biggerstr.append(insertstr.charAt(i));
45 }
46
47 //instead_zero字符串用于存放清除零之后的
48 String instead_zero=biggerstr.reverse().toString();
49 instead_zero=instead_zero.replaceAll("零分", "");
50 instead_zero=instead_zero.replaceAll("零角", "");
51 instead_zero=instead_zero.replaceAll("零佰", "");
52 instead_zero=instead_zero.replaceAll("零仟", "");
53 instead_zero=instead_zero.replaceAll("零拾", "");
54 //把相连的零去掉
55 instead_zero=instead_zero.replaceAll("[零]+","");
56 instead_zero=instead_zero.replaceAll("零元", "");
57 instead_zero=instead_zero.replaceAll("零万", "");
58 instead_zero=instead_zero.replaceAll("零亿", "亿");
59 //把字符串中的最后一个零去掉
60 if(instead_zero.charAt(instead_zero.length()-1)==''){
61 instead_zero=instead_zero.substring(0, instead_zero.length()-1);
62 }
63 System.out.println(instead_zero);
64 }
65 public static void main(String[] args) {
66 String smoney="1020089090.89";
67 change(smoney);
68 }
69 }
2

View Code
1 // <summary>
2 /// 金钱转换
3 /// </summary>
4 /// <param name="num"></param>
5 /// <returns></returns>
6 public static string ConvertToCn(decimal num)
7 {
8 string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
9 string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
10 string str3 = ""; //从原num值中取出的值
11 string str4 = ""; //数字的字符串形式
12 string str5 = ""; //人民币大写金额形式
13 int i; //循环变量
14 int j; //num的值乘以100的字符串长度
15 string ch1 = ""; //数字的汉语读法
16 string ch2 = ""; //数字位的汉字读法
17 int nzero = 0; //用来计算连续的零值是几个
18 int temp; //从原num值中取出的值
19
20 num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
21 str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
22 j = str4.Length; //找出最高位
23 if (j > 15) { return "溢出"; }
24 str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
25
26 //循环取出每一位需要转换的值
27 for (i = 0; i < j; i++)
28 {
29 str3 = str4.Substring(i, 1); //取出需转换的某一位的值
30 temp = Convert.ToInt32(str3); //转换为数字
31 if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
32 {
33 //当所取位数不为元、万、亿、万亿上的数字时
34 if (str3 == "0")
35 {
36 ch1 = "";
37 ch2 = "";
38 nzero = nzero + 1;
39 }
40 else
41 {
42 if (str3 != "0" && nzero != 0)
43 {
44 ch1 = "" + str1.Substring(temp * 1, 1);
45 ch2 = str2.Substring(i, 1);
46 nzero = 0;
47 }
48 else
49 {
50 ch1 = str1.Substring(temp * 1, 1);
51 ch2 = str2.Substring(i, 1);
52 nzero = 0;
53 }
54 }
55 }
56 else
57 {
58 //该位是万亿,亿,万,元位等关键位
59 if (str3 != "0" && nzero != 0)
60 {
61 ch1 = "" + str1.Substring(temp * 1, 1);
62 ch2 = str2.Substring(i, 1);
63 nzero = 0;
64 }
65 else
66 {
67 if (str3 != "0" && nzero == 0)
68 {
69 ch1 = str1.Substring(temp * 1, 1);
70 ch2 = str2.Substring(i, 1);
71 nzero = 0;
72 }
73 else
74 {
75 if (str3 == "0" && nzero >= 3)
76 {
77 ch1 = "";
78 ch2 = "";
79 nzero = nzero + 1;
80 }
81 else
82 {
83 if (j >= 11)
84 {
85 ch1 = "";
86 nzero = nzero + 1;
87 }
88 else
89 {
90 ch1 = "";
91 ch2 = str2.Substring(i, 1);
92 nzero = nzero + 1;
93 }
94 }
95 }
96 }
97 }
98 if (i == (j - 11) || i == (j - 3))
99 {
100 //如果该位是亿位或元位,则必须写上
101 ch2 = str2.Substring(i, 1);
102 }
103 str5 = str5 + ch1 + ch2;
104
105 if (i == j - 1 && str3 == "0")
106 {
107 //最后一位(分)为0时,加上“整”
108 str5 = str5;
109 }
110 }
111 if (num == 0)
112 {
113 str5 = "零元";
114 }
115 return str5;
116 }
3

class Program
    {
         private static string[] intStr = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
         private static string[] numStr = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
         private static string[] moneyPattern = new string[] { "", "拾", "佰", "仟", "万", "亿" };
        static void Main(string[] args)
        {
            string money = Console.ReadLine().ToString();
            string str=BindMoney(money);
            Console.WriteLine(str);
            Console.ReadLine();
        }
        public static string BindMoney(string money)
        {
            string str = "";           
            for (int i = 0; i < money.Length; i++)
            {
                for (int j = 0; j < intStr.Length; j++)
                {
                    if (money.Substring(i,1).Equals(intStr[j]))
                    {
                        str += numStr[j];
                    }
                }
            }
            str = BindMoneyPattern(str);
            str = BindMoneyPattern2(str);
           return str;
        }
        private static string BindMoneyPattern(string str)
        {
            string moneyStr = "";
            for (int i = 0; i < str.Length; i++)
            {
                if ((str.Length - i) % 4 == 0)
                {
                    moneyStr += str[i] + moneyPattern[3];
                }
                else if ((str.Length - i) % 4 == 2)
                {
                    moneyStr += str[i] + moneyPattern[1];
                }
                else if ((str.Length - i) % 4 == 3)
                {
                    moneyStr += str[i] + moneyPattern[2];
                }
                else if ((str.Length - i) % 5 == 0)
                {
                    moneyStr += str[i] + moneyPattern[4];
                }
                else if ((str.Length-i)%9==0)
                {
                    moneyStr += str[i] + moneyPattern[5];
                }
                else
                {
                    moneyStr += str[i];
                }
            }
            return moneyStr;
        }
        private static string BindMoneyPattern2(string str)
        {
            while (str.Contains("零拾"))
            {
                str=str.Replace("零拾","零");
            }
            while (str.Contains("零佰"))
            {
                str = str.Replace("零佰", "零");
            }
            while (str.Contains("零仟"))
            {
                str = str.Replace("零仟", "零");
            }
            while (str.Contains("零万"))
            {
                str = str.Replace("零万","万");
            }
            while (str.Contains("零亿"))
            {
                str = str.Replace("零亿", "亿");
            }
            while (str.Contains("亿万"))
            {
                str = str.Replace("亿万", "亿");
            }
            while (str.Contains("零零"))
            {
                str = str.Replace("零零", "零");
            }
            while (str.IndexOf("零")==str.Length-1)
            {
                str = str.Replace("零","");
            }
            return str;
        }
    }

4

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 //转换数字金额为人民币大写
6 namespace ConvertMoney
7 {
8 public class ConvertMoney
9 {
10 /// <summary>
11 /// 是否是浮点数 可带正负号
12 /// </summary>
13 /// <param name="inputData">输入字符串 </param>
14 /// <returns> </returns>
15 //public static bool IsDecimalSign(string inputData)
16 //{
17 // Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
18 // Match m = RegDecimalSign.Match(inputData);
19 // return m.Success;
20 //}
21
22 static public string convertMoneytoRMB(decimal decMoney)
23 {
24 string strMoney,strOneNum,strTemp,strConverted;
25 int i,iLen;
26
27 //设初值
28 strConverted = "";
29 strMoney = decMoney.ToString();
30 iLen = strMoney.Length;
31
32 //先取小数位
33 if (strMoney.IndexOf(".") > 0)
34 {
35 strTemp = strMoney.Substring(strMoney.IndexOf(".") + 1, strMoney.Length - strMoney.IndexOf(".") - 1);
36 if (strTemp.Length > 2)
37 {
38 Console.WriteLine("错误:无法计算超过2位的小数");
39 return strConverted;
40 }
41 else if (strTemp == "0" || strTemp == "00" || strTemp == "")
42 strTemp = "";
43 else
44 {
45 if (strTemp.Length == 1 && strTemp != "0")
46 {
47 strConverted = converNumtoCapital(strTemp) + "" + strConverted;
48 }
49 else
50 {
51 strOneNum = strTemp.Substring(0, 1);
52 strConverted = converNumtoCapital(strOneNum) + (strOneNum != "0" ? "" : "") + strConverted;
53 strOneNum = strTemp.Substring(1, 1);
54 strConverted = strConverted + (strOneNum != "0" ? converNumtoCapital(strOneNum) + "" : "");
55 }
56 }
57 }
58
59 //取整数部分
60 if (strMoney.IndexOf(".") < 0)
61 strTemp = strMoney;
62 else
63 strTemp = strMoney.Substring(0, strMoney.IndexOf("."));
64
65 iLen = strTemp.Length;
66 //Console.WriteLine(iLen);
67 Console.WriteLine(strTemp);
68
69 if (iLen > 0 && decimal.Parse(strTemp) != 0)
70 {
71 strConverted = "" + strConverted;
72
73 for (i = 0; i < iLen; ++i)
74 {
75 strOneNum = strTemp.Substring(iLen - 1 - i, 1);
76 //if (strOneNum == "0")
77 //{
78 // //Console.WriteLine(strConverted.Substring(0, 1));
79 // if ((strConverted.Substring(0, 1) == "零" || strConverted.Substring(0, 1) == "元" || strConverted.Substring(0, 1) == "万" || strConverted.Substring(0, 1) == "亿") && !((i + 1) % 12 == 0 || (i + 1) == 5 || (i + 1) % 9 == 0))
80 // continue;
81 // else
82 // strConverted = converNumtoCapital(strOneNum) + strConverted;
83 //}
84
85 //Console.WriteLine((i + 1) % 4);
86
87 if ((i + 1) == 1)
88 {
89 strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + strConverted;
90 }
91 else if (((i + 1) % 4 == 2 || (i + 1) == 2) && i % 4 != 0 && i % 8 != 0)
92 {
93 if (strOneNum == "0")
94 {
95 if (strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "亿")
96 continue;
97 else
98 strConverted = "" + strConverted;
99 }
100 else
101 strConverted = converNumtoCapital(strOneNum) + "" + strConverted;
102 }
103 else if (((i + 1) % 4 == 3 || (i + 1) == 3) && i % 4 != 0)
104 {
105 if (strOneNum == "0")
106 {
107 if (strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "亿")
108 continue;
109 else
110 strConverted = "" + strConverted;
111 }
112 else
113 strConverted = converNumtoCapital(strOneNum) + "" + strConverted;
114 }
115 else if ((i + 1) % 4 == 0 && i % 4 != 0)
116 {
117 if (strOneNum == "0")
118 {
119 if (strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "" || strConverted.Substring(0, 1) == "亿")
120 continue;
121 else
122 strConverted = "" + strConverted;
123 }
124 else
125 strConverted = converNumtoCapital(strOneNum) + "" + strConverted;
126 }
127 else if (i % 4 == 0 && i % 8 != 0)
128 {
129 Console.WriteLine("万位{0}", i);
130 strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "" + strConverted;
131 }
132 else if (i % 8 == 0)
133 {
134 Console.WriteLine("亿位{0}", i);
135 if (strConverted.Substring(0, 1) == "") strConverted = strConverted.Substring(1, strConverted.Length - 1);
136 strConverted = (strOneNum == "0" ? "" : converNumtoCapital(strOneNum)) + "亿" + strConverted;
137 }
138 else
139 {
140 Console.WriteLine(i);
141 strConverted = converNumtoCapital(strOneNum) + strConverted;
142 }
143 }
144 }
145
146 return strConverted;
147 }
148
149 static string converNumtoCapital(string strNum)
150 {
151 string strCapital = "";
152 switch (strNum)
153 {
154 case "0":
155 strCapital = "";
156 break;
157 case "1":
158 strCapital = "";
159 break;
160 case "2":
161 strCapital = "";
162 break;
163 case "3":
164 strCapital = "";
165 break;
166 case "4":
167 strCapital = "";
168 break;
169 case "5":
170 strCapital = "";
171 break;
172 case "6":
173 strCapital = "";
174 break;
175 case "7":
176 strCapital = "";
177 break;
178 case "8":
179 strCapital = "";
180 break;
181 case "9":
182 strCapital = "";
183 break;
184 default:
185 strCapital = "";
186 break;
187 }
188 return strCapital;
189 }
190 }
191
192 class Program
193 {
194 static void Main(string[] args)
195 {
196 decimal i;
197 string s;
198 while (1 == 1)
199 {
200 Console.WriteLine("输入一个金额(直接回车退出):");
201 s = Console.ReadLine();
202 if (s == "") break;
203 //检查金额是否符合规则
204 try
205 {
206 i = decimal.Parse(s);
207 if (i < 0)
208 throw new Exception("不能是负数");
209 }
210 catch (Exception e)
211 {
212 Console.WriteLine(e.Message);
213 return;
214 }
215
216 /*
217 if (!(ConvertMoney.IsDecimalSign(s)))
218 Console.WriteLine("错误:不是金额!");
219 */
220
221 //i = Convert.ToDecimal(s);
222 s = ConvertMoney.convertMoneytoRMB(i);
223 Console.WriteLine("人民币大写金额为:{0}", s);
224 }
225 }
226 }
227 }

 
posted @ 2011-05-31 00:03  Credo Chen  阅读(1749)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量