【转】.net将一串价格数字转化为中文大写形式

  • 【转】.net将一串价格数字转化为中文大写形式已关闭评论
  • 457 views
  • A+
所属分类:C#.NET 编程技术
【腾讯云】11.11 云上盛惠,云产品限时抢购,1核2G云服务器首年88元

今天工作时要实现一个报价单,里面要填写一串汉字大写金额

在网上找到如下方法,分享给大家,另一个版本的试过用不了。

将该方法进行封装为PriceChange方法,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
 
namespace AutomatedValuation.MyDBClass
{
    public class PriceChange
    {
        public string[] strPrice;
        public string[] strRMB;
        public string strMast = string.Empty;
        public PriceChange()
        {
            //数字的汉字读法初始化
            strPrice = new string[] { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆" };
            strRMB = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };
            strMast = "万亿";
        }
        /// <summary>
        /// 将人民币的数字形式转换成汉字
        /// </summary>
        /// <param name="strMoney">人民币值的数字表示(字符串)</param>
        /// <returns>数字的汉字读法</returns>
        public string PCget(string strMoney)
        {
            try
            {
                if (!DataChk(strMoney))//调用自己写的是否是数字类型的判断函数
                {
                    throw new System.ArgumentException("人民币数值类型错误!");
                }
                //是否已做了整数部分处理(FALSE:NO,TRUE: YES)
                Boolean blnOne = false;
                //数字转成汉字返回字符串初始化
                string strPCget = string.Empty;
                //整数与小数部分分隔符
                char[] strFG = { '.' };
                //将数字的整数与小数分成两组
                string[] strM = strMoney.Split(strFG);
                //数字向汉字转换处理
                foreach (string s1 in strM)
                {
                    //将数字中","去除
                    string strM2 = s1.Replace(",", "").Replace(",", "");
                    //求整数部分位数
                    int intCount = strM2.Length;
                    if (intCount > 13)
                    {
                        throw new System.ArgumentException("人民币数值位数超出本程序处理的范围。人民币最大值到“万亿”。");
                    }
                    //取得人民币数字单位读法的数组索引
                    int intC = intCount + 1;
                    //整数部分转换处理
                    if (blnOne == false)
                    {
                        for (int i = 0; i < intCount; i++)
                        {
                            blnOne = true;
                            //数字读法和人民币单位组合
                            strPCget = strPCget + strRMB[Convert.ToInt32(strM2.Substring(i, 1))] + strPrice[intC].ToString();
                            intC = intC - 1;
                        }
                    }
                    //小数部分转换处理
                    else
                    {
                        for (int i = 0; i < intCount; i++)
                        {
                            //数字读法取得
                            strPCget = strPCget + strRMB[Convert.ToInt32(strM2.Substring(i, 1))] + strPrice[intC - 2].ToString();
                            intC = intC - 1;
                        }
                    }
 
                }
                strPCget = this.StrForm(strPCget);
                return strPCget;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 格式整理
        /// </summary>
        /// <param name="strFrom"></param>
        /// <returns></returns>
        private string StrForm(string strFrom)
        {
            bool blHave = true;
            while (blHave)
            {
                int intPosition = strFrom.IndexOf("零");
                if (intPosition != -1)
                {
                    if (strMast.Replace(strFrom.Substring(intPosition + 1, 1), "").Length < strMast.Length)
                    //&& strFrom.Substring(intPosition - 1) == "零")
                    {
                        if (strMast.Replace(strFrom.Substring(intPosition - 1, 1), "").Length < strMast.Length)
                        {
                            strFrom = strFrom.Remove(intPosition, 2);
                        }
                        else
                        {
                            strFrom = strFrom.Remove(intPosition, 1);
                        }
                    }
                    else
                    {
                        strFrom = strFrom.Remove(intPosition, 2);
                    }
                }
                else
                {
                    blHave = false;
                }
            }
            return strFrom;
        }
        /// <summary>
        /// 是否是数字类型
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        public static Boolean DataChk(string strData)
        {
            double intData;
            if (double.TryParse(strData.Replace(",", "").Replace(",", "").Replace(".", ""), out intData))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin