首页 > 代码库 > .NET下金额大小写转换

.NET下金额大小写转换

原文:.NET下金额大小写转换

说明:金额转换可以转换50位的数值,单位从分到级。对于中间部分是否显示零,
可以根据修改TranslateJInen()函数修改。
中间数值为零的去掉不显示
 public string GetChCapitalNumber(string canshu)

中间数值为零的显示
public string GetChCapitalNumberFill(string canshu)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Jinen_Translate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //判断粘贴而来的数据是否满足条件,只能为数字和小数点
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

        //计算小数点之后一共有几位数字  2 结果为0  3.258 结果为3
        //超出十位的小数点记录为10位
        public int Fun_getLengthPoint(string inputText)
        {
            int PointLength = 0;
            int count = inputText.Length;
            //记录输入字符串的长度
            int jilu = 0;
            /*记录小数点所在的位置
            当出现小数点时,停止循环*/
            for (int i = 0; i < count; i++)
            {
                jilu++;
                if (inputText[i] == ‘.‘)
                    break;
            }


            if (jilu == count)
            {
                PointLength = 0;
                return PointLength;
            }

            //记录输入字符串的长度
            else
            {
                int dian_point = inputText.IndexOf(".");
                //采集小数点以后的字符串。   int dian = input.IndexOf(".");记录小数点所在的位置
                string base_last = inputText.Substring(dian_point + 1, count - jilu);
                //记录小数点之后数的位数
                PointLength = base_last.Length;
                //返回小数点一共有几位数字
                return PointLength;
            }
        }


        //Dupliacte() 创建重复字符串
        public string Duplicate(string mstr, int ncnt)
        {
            // 使用 StringBuilder 加速字符串重复创建的速度
            StringBuilder dstr = new StringBuilder();
            int icnt = 0;

            for (icnt = 0; icnt < ncnt; icnt++)
            {
                dstr.Append(mstr);
            }

            return dstr.ToString();
        }

        //截取字符串
        public string Left(string mdata, int leng)
        {
            mdata = http://www.mamicode.com/mdata.Trim();
            if (mdata.Length < leng)
                leng = mdata.Length;

            return mdata.Trim().Substring(0, leng);
        }

        //取得所有的字符串
        public String Fun_getText(string inputText)
        {
            //记录输入字符串的长度
            int jilu = 0;
            int count = inputText.Length;
            /*记录小数点所在的位置
            当出现小数点时,停止循环*/
            for (int i = 0; i < count; i++)
            {
                jilu++;
                if (inputText[i] == ‘.‘)
                    break;
            }

            if (jilu == count)
            {
                string number1 = inputText;
                return number1;
            }
            else
            {
                //采集小数点以前的字符串
                string base_hade = inputText.Substring(0, jilu - 1);
                int dian_point = inputText.IndexOf(".");
                //采集小数点以后的字符串。   int dian = input.IndexOf(".");记录小数点所在的位置
                string base_last = inputText.Substring(dian_point + 1, count - jilu);
                //形成新的字符串,去掉了小数点
                string number1 = base_hade + base_last;
                return number1;
            }
        }
    
        // #region GetFourChNumber() 取得每四位数的中文位数字 (万、亿、兆...)
        public string GetFourChNumber(int iDigit)
        {
            string sDigit = "";

            // 限于整数态的上限,‘京’之后的中文数字是用不到
            switch (iDigit)
            {
                case 0:
                    sDigit = "";
                    break;
                case 1:
                    sDigit = "万";
                    break;
                case 2:
                    sDigit = "亿";
                    break;
                case 3:
                    sDigit = "兆";
                    break;
                case 4:
                    sDigit = "京";
                    break;
                case 5:
                    sDigit = "垓";
                    break;
                case 6:
                    sDigit = "秭";
                    break;
                case 7:
                    sDigit = "穰";
                    break;
                case 8:
                    sDigit = "沟";
                    break;
                case 9:
                    sDigit = "涧";
                    break;
                case 10:
                    sDigit = "正";
                    break;
                case 11:
                    sDigit = "载";
                    break;
                case 12:
                    sDigit = "极";
                    break;
                default:
                    sDigit = "∞";
                    break;
            }

            return sDigit;
        }

        //获得数据的一个一个变为大写数字
        public string ChCapitalNumber(string NStr)
        {
            NStr = Left(NStr, 1);

            switch (NStr)
            {
                case "0":
                    NStr = "零";
                    break;

                case "1":
                    NStr = "壹";
                    break;

                case "2":
                    NStr = "贰";
                    break;

                case "3":
                    NStr = "参";
                    break;

                case "4":
                    NStr = "肆";
                    break;

                case "5":
                    NStr = "伍";
                    break;

                case "6":
                    NStr = "陆";
                    break;

                case "7":
                    NStr = "柒";
                    break;

                case "8":
                    NStr = "捌";
                    break;

                case "9":
                    NStr = "玖";
                    break;

                default:
                    NStr = "?";
                    break;
            }
            return NStr;
        }

        //省略零的结果转换
        public string GetChCapitalNumber(string NumStr)
        {
            string ChStr = "", ChSubStr = "", tmpStr = "";
            int iCnt = 0, jCnt = 0, kCnt = 0, lCnt = -1, nCnt = 0;
            nCnt = NumStr.Length;

            // 中文数字以四位数为一个处理单位(万、亿、兆、京....)
            iCnt = nCnt % 4;
            NumStr = Duplicate("0", 4 - iCnt) + NumStr;
            nCnt = NumStr.Length;

            for (iCnt = 0; iCnt < nCnt; iCnt += 4)
            {
                lCnt++;
                ChSubStr = "";

                for (jCnt = 0; jCnt < 4; jCnt++)
                {
                    kCnt = nCnt - iCnt - jCnt - 1;
                    tmpStr = ChCapitalNumber(NumStr.Substring(kCnt, 1));

                    if (tmpStr == "零")
                    {
                        if (Left(ChSubStr, 1) != "零" && Left(ChSubStr, 1) != "")
                            ChSubStr = tmpStr + ChSubStr;
                    }
                    else
                    {
                        switch (jCnt)
                        {
                            case 0:
                                ChSubStr = tmpStr;
                                break;
                            case 1:
                                ChSubStr = tmpStr + "拾" + ChSubStr;
                                break;
                            case 2:
                                ChSubStr = tmpStr + "佰" + ChSubStr;
                                break;
                            case 3:
                                ChSubStr = tmpStr + "仟" + ChSubStr;
                                break;
                            default:
                                ChSubStr = tmpStr + ChSubStr;
                                break;
                        }
                    }
                }

                if (ChSubStr == "零" && Left(ChStr, 1) != "" && Left(ChStr, 1) != "零")
                    ChStr = "零" + ChStr;
                else
                {
                    if (ChSubStr != "")
                    {
                        // 取得10000的次方中文数字
                        // 限于整数态的上限,‘京’之后的中文数字是用不到
                        ChStr = ChSubStr + GetFourChNumber(lCnt) + ChStr;
                    }
                }
            }

            if (ChStr == "")
                ChStr = "零";
            else if (Left(ChStr, 1) == "零")
                ChStr = ChStr.Substring(1, ChStr.Length - 1);

            return ChStr;
        }

        //包括零的结果运算
        public string GetChCapitalNumberFill(string NumStr)
        {
            string ChStr = "", ChSubStr = "", tmpStr = "";
            int iCnt = 0, jCnt = 0, kCnt = 0, lCnt = -1, nCnt = 0;
            nCnt = NumStr.Length;

            // 中文数字以四位数为一个处理单位(万、亿、兆、京....)
            iCnt = nCnt % 4;
            NumStr = Duplicate("0", 4 - iCnt) + NumStr;
            nCnt = NumStr.Length;

            for (iCnt = 0; iCnt < nCnt; iCnt += 4)
            {
                lCnt++;
                ChSubStr = "";

                for (jCnt = 0; jCnt < 4; jCnt++)
                {
                    kCnt = nCnt - iCnt - jCnt - 1;
                    tmpStr = ChCapitalNumber(NumStr.Substring(kCnt, 1));

                    switch (jCnt)
                    {
                        case 0:
                            ChSubStr = tmpStr;
                            break;
                        case 1:
                            ChSubStr = tmpStr + "拾" + ChSubStr;
                            break;
                        case 2:
                            ChSubStr = tmpStr + "佰" + ChSubStr;
                            break;
                        case 3:
                            ChSubStr = tmpStr + "仟" + ChSubStr;
                            break;
                        default:
                            ChSubStr = tmpStr + ChSubStr;
                            break;
                    }
                }

                if (ChSubStr != "")
                {
                    // 取得10000的次方中文数字
                    // 限于整数态的上限,‘京’之后的中文数字是用不到
                    ChStr = ChSubStr + GetFourChNumber(lCnt) + ChStr;
                }
            }

            while (Left(ChStr, 1) == "零" && ChStr.Length > 1)
            {
                if (Left(ChStr, 1) == "零")
                    ChStr = ChStr.Substring(2, ChStr.Length - 2);
            }

            return ChStr;
        }
       // 金额转换
        public String TranslateJinen(string canshu)
        {
            string result;
            if (!IsNumeric(canshu))
            {
                result = "Error!";
                return result;
            }
            else
            {
                if (canshu.Length > 50)
                {
                    result = "数值太大,转换失败!";
                    return result;
                }
                else
                {
                    int t = Fun_getLengthPoint(canshu);  //t为传人参数的小数点后面的位数 如2.56 t=2
                    if (t == 0)
                    {
                        string jin = Fun_getText(canshu);
                        jin = GetChCapitalNumberFill(canshu);
                        string Tran_jin = jin + "元零角零分";
                        return Tran_jin;
                    }
                    else
                    {
                        string jin = Fun_getText(canshu) + "00";                 //以防只输入一位小数,人为地在后面加两个0
                        string jin_head = jin.Substring(0, jin.Length - 2 - t);      //截取小数点以后的两位数值                   
                        string jin_last_f = jin.Substring(jin_head.Length, 1);      //截取小数点之后的第一位数值  如45.235 结果为2
                        string jin_last_l = jin.Substring(jin_head.Length + 1, 1); //截取小数点之后的第二位数值  如45.235 结果为3
                        jin_head = GetChCapitalNumberFill(jin_head);
                        jin_last_f = ChCapitalNumber(jin_last_f);
                        jin_last_l = ChCapitalNumber(jin_last_l);
                        string Tran_jin = jin_head + "元" + jin_last_f + "角" + jin_last_l + "分";
                        return Tran_jin;
                    }
                }
            }

        }

        private void Action_Click(object sender, EventArgs e)
        {
            DaXieJinen.Text = TranslateJinen(Jinen.Text);
        }
    }

.NET下金额大小写转换