c#人民币金额大写数字互相转换

用c#编辑的人民币大写金额和数字之间的转换代码,可实现10的20次方以下大小中文金额大写和数字的转换。
以下是数字小写转中文大写代码:

using System;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RMBdzx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string RMBtran(string LowerMoney)//数字转中文大写
        {
            string RMBtranValue = null;
            string strLower = null;
            string strUpart = null;
            string strUpper = null;
            LowerMoney = Regex.Replace(LowerMoney, @"[^0-9.]", "");//去掉字符串中除数字和小数点之外的字符
            if (LowerMoney.Contains("."))
            {
                int firstIndex = LowerMoney.IndexOf("."); // 找到第一个.号的索引  
                LowerMoney = LowerMoney.Replace(".", ""); // 移除所有点
                LowerMoney = LowerMoney.Insert(firstIndex, ".");// 在原位第一个点位插入点
            }
            //数据整理正数为不超过24为,小数点四舍五入,超过的从左到右截取
            if (LowerMoney.IndexOf(".") == 0)
            {
                if (LowerMoney.Length >= 4)
                {
                    LowerMoney = LowerMoney.Substring(0, 4);
                }
            }
            else if (LowerMoney.IndexOf(".") < 0)
            {
                if (LowerMoney.Length >= 24)
                {
                    LowerMoney = LowerMoney.Substring(0, 24);
                }
            }
            else if (LowerMoney.IndexOf(".") > 0)
            {
                if (LowerMoney.IndexOf(".") <= 24)
                {
                    if (LowerMoney.IndexOf(".") + 4 <= LowerMoney.Length)
                    {
                        LowerMoney = LowerMoney.Substring(0, LowerMoney.IndexOf(".") + 4);
                    }
                }
                else if (LowerMoney.IndexOf(".") > 24)
                {
                    LowerMoney = LowerMoney.Substring(0, 24);
                }
            }
            if (LowerMoney == "" || LowerMoney == ".")
            { LowerMoney = "0"; }
            decimal decl = decimal.Parse(LowerMoney);            //将字符串转化为decimal类型
            string str = String.Format(CultureInfo.InvariantCulture, "{0:N2}", decl);//格式化为字符串千位符、四舍五入两位小数
            LowerMoney = str.Replace(",", "");          //去掉千位符
            strLower = LowerMoney;
            int iTemp = 1;
            strUpper = "";
            while (iTemp <= strLower.Length)
            {
                switch (strLower.Substring(strLower.Length - iTemp, 1))
                {
                    case ".":
                        strUpart = "元";
                        break;
                    case "0":
                        strUpart = "零";
                        break;
                    case "1":
                        strUpart = "壹";
                        break;
                    case "2":
                        strUpart = "贰";
                        break;
                    case "3":
                        strUpart = "叁";
                        break;
                    case "4":
                        strUpart = "肆";
                        break;
                    case "5":
                        strUpart = "伍";
                        break;
                    case "6":
                        strUpart = "陆";
                        break;
                    case "7":
                        strUpart = "柒";
                        break;
                    case "8":
                        strUpart = "捌";
                        break;
                    case "9":
                        strUpart = "玖";
                        break;
                }

                switch (iTemp)
                {
                    case 1:
                        strUpart = strUpart + "分";
                        break;
                    case 2:
                        strUpart = strUpart + "角";
                        break;
                    case 3:
                        strUpart = strUpart + "";
                        break;
                    case 4:
                        strUpart = strUpart + "";
                        break;
                    case 5:
                        strUpart = strUpart + "拾";
                        break;
                    case 6:
                        strUpart = strUpart + "佰";
                        break;
                    case 7:
                        strUpart = strUpart + "仟";
                        break;
                    case 8:
                        strUpart = strUpart + "万";
                        break;
                    case 9:
                        strUpart = strUpart + "拾";
                        break;
                    case 10:
                        strUpart = strUpart + "佰";
                        break;
                    case 11:
                        strUpart = strUpart + "仟";
                        break;
                    case 12:
                        strUpart = strUpart + "亿";
                        break;
                    case 13:
                        strUpart = strUpart + "拾";
                        break;
                    case 14:
                        strUpart = strUpart + "佰";
                        break;
                    case 15:
                        strUpart = strUpart + "仟";
                        break;
                    case 16:
                        strUpart = strUpart + "兆";
                        break;
                    case 17:
                        strUpart = strUpart + "拾";
                        break;
                    case 18:
                        strUpart = strUpart + "佰";
                        break;
                    case 19:
                        strUpart = strUpart + "仟";
                        break;
                    case 20:
                        strUpart = strUpart + "京";
                        break;
                    case 21:
                        strUpart = strUpart + "拾";
                        break;
                    case 22:
                        strUpart = strUpart + "佰";
                        break;
                    case 23:
                        strUpart = strUpart + "仟";
                        break;
                    case 24:
                        strUpart = strUpart + "垓";
                        break;
                    case 25:
                        strUpart = strUpart + "拾";
                        break;
                    case 26:
                        strUpart = strUpart + "佰";
                        break;
                    case 27:
                        strUpart = strUpart + "仟";
                        break;
                    default:
                        strUpart = strUpart + "";
                        break;
                }

                strUpper = strUpart + strUpper;
                iTemp = iTemp + 1;
            }
            strUpper = strUpper.Replace("零拾", "零");
            strUpper = strUpper.Replace("零佰", "零");
            strUpper = strUpper.Replace("零仟", "零");
            strUpper = strUpper.Replace("零零零", "零");
            strUpper = strUpper.Replace("零零", "零");
            strUpper = strUpper.Replace("零角零分", "整");
            strUpper = strUpper.Replace("零分", "整");
            strUpper = strUpper.Replace("零角", "零");
            strUpper = strUpper.Replace("零垓", "垓");
            strUpper = strUpper.Replace("零京", "京");
            strUpper = strUpper.Replace("零兆", "兆");
            strUpper = strUpper.Replace("零亿", "亿");
            strUpper = strUpper.Replace("零万", "万");
            strUpper = strUpper.Replace("垓京", "垓");
            strUpper = strUpper.Replace("垓兆", "垓");
            strUpper = strUpper.Replace("垓亿", "垓");
            strUpper = strUpper.Replace("垓万", "垓");
            strUpper = strUpper.Replace("京兆", "京");
            strUpper = strUpper.Replace("京亿", "京");
            strUpper = strUpper.Replace("京万", "京");
            strUpper = strUpper.Replace("兆亿", "兆");
            strUpper = strUpper.Replace("兆万", "兆");
            strUpper = strUpper.Replace("亿万", "亿");
            strUpper = strUpper.Replace("零元", "元");
            strUpper = strUpper.Replace("零零", "零");
            // 对壹元以下的金额的处理
            if (strUpper.Substring(0, 1) == "元")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "零")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "角")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "分")
            {
                strUpper = strUpper.Substring(1, strUpper.Length - 1);
            }
            if (strUpper.Substring(0, 1) == "整")
            {
                strUpper = "零元整";
            }
            RMBtranValue = strUpper;
            return RMBtranValue;
        }
 private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            if (textBox1.Text == "")
            {
                MessageBox.Show("空值不能转写,请正确输入!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
                return;
            }
            decimal decl = decimal.Parse(LowMoney);            //将字符串转化为decimal类型
            string str1 = String.Format(CultureInfo.InvariantCulture, "{0:N2}", decl);//格式化为字符串千位符、四舍五入两位小数
            textBox1.Text = str1;
        }
     }
}

以下是中文大写转数字小写代码:

using System;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RMBdzx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
                public static string RMBTranNum(string ConUpperStr)
        {
            String PutValue = "";//输出变量
            ConUpperStr = ConUpperStr.Replace("万万","亿");
            ConUpperStr = ConUpperStr.Replace("万亿", "兆");
            ConUpperStr = ConUpperStr.Replace("万兆", "京");
            ConUpperStr = ConUpperStr.Replace("万京", "垓");
            ConUpperStr = Regex.Replace(ConUpperStr, @"[^壹贰叁肆伍陆柒捌玖零垓京兆亿万仟佰拾元角分整正]", "");
            string Converstr = ConUpperStr;
            string MoConverstr = "";
            decimal ConNum = 0;
            decimal UppeNnum = 0;
            decimal SumNnum = 0;
            int[] szarr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,0 };
            string[] zwdxarr = new string[] { "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖","零" };
            string[] yqdwarr = new string[] { "垓", "京", "兆", "亿", "万" };
            int[] yqpfarr = new int[] { 20, 16, 12, 8, 4 };
            string[] wqdwarr = new string[] { "仟", "佰", "拾", "元", "角", "分" };
            int[] wqpfarr = new int[] { 3, 2, 1, 0, -1, -2 };
            Converstr = Converstr.Replace("零", "");
            Converstr = Converstr.Replace("正", "");
            Converstr = Converstr.Replace("整", "");
            string BeConverstr = Converstr;
            bool contains = false;
            foreach (string s in yqdwarr)
            {
                if (Converstr.Contains(s))
                {
                    contains = true;
                    break;
                }
            }
            if (contains)//转换万元及以上的大写
            {
                for (int i = 0; i <= yqdwarr.Length - 1; i++)   //转换单位在万及以上的部分
                {
                    if (Converstr.IndexOf(yqdwarr[i]) != -1)
                    {
                        MoConverstr = Converstr.Substring(Converstr.IndexOf(yqdwarr[i]) + 1, Converstr.Length - Converstr.IndexOf(yqdwarr[i]) - 1);//截取中文单位后面的字符串
                        Converstr = Converstr.Substring(0, Converstr.IndexOf(yqdwarr[i]) + 1);
                        if ((Converstr.Length - 1) % 2 == 0)
                        {
                            for (int j = 0; j < Converstr.Length - 1; j = j + 2)
                            {
                                UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, Converstr[j].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, Converstr[j + 1].ToString())]);
                                SumNnum = SumNnum + UppeNnum;
                            }
                        }
                        else
                        {
                            for (int j = 0; j < Converstr.Length - 2; j = j + 2)
                            {
                                UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, Converstr[j].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, Converstr[j + 1].ToString())]);
                                SumNnum = SumNnum + UppeNnum;
                            }
                            SumNnum = SumNnum + (decimal)szarr[Array.IndexOf(zwdxarr, Converstr[Converstr.Length - 2].ToString())];
                        }
                        SumNnum = SumNnum * (decimal)Math.Pow(10, yqpfarr[Array.IndexOf(yqdwarr, Converstr[Converstr.Length - 1].ToString())]);
                        ConNum = ConNum + SumNnum;
                        SumNnum = 0;
                        UppeNnum = 0;
                        Converstr = MoConverstr;
                    }
                }
                if (MoConverstr.Length % 2 == 0)//转换单位在万以下的部分
                {
                    for (int i = 0; i < MoConverstr.Length - 1; i = i + 2)
                    {
                        UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, MoConverstr[i].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, MoConverstr[i + 1].ToString())]);
                        SumNnum = SumNnum + UppeNnum;
                    }
                }
                else
                {
                    MoConverstr = MoConverstr.Replace("元", "");
                    for (int i = 0; i < MoConverstr.Length - 1; i = i + 2)
                    {
                        UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, MoConverstr[i].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, MoConverstr[i + 1].ToString())]);
                        SumNnum = SumNnum + UppeNnum;
                    }
                }
                ConNum = ConNum + SumNnum;

            }
            else //转换万元以下的大写
            {
                if (BeConverstr.Length % 2 == 0)
                {
                    for (int i = 0; i < BeConverstr.Length - 1; i = i + 2)
                    {
                        UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, BeConverstr[i].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, BeConverstr[i + 1].ToString())]);
                        SumNnum = SumNnum + UppeNnum;
                    }
                    ConNum = ConNum + SumNnum;
                }
                else
                {
                    BeConverstr = BeConverstr.Replace("元", "");
                    for (int i = 0; i < BeConverstr.Length - 1; i = i + 2)
                    {
                        UppeNnum = (decimal)szarr[Array.IndexOf(zwdxarr, BeConverstr[i].ToString())] * (decimal)Math.Pow(10, wqpfarr[Array.IndexOf(wqdwarr, BeConverstr[i + 1].ToString())]);
                        SumNnum = SumNnum + UppeNnum;
                    }
                    ConNum = ConNum + SumNnum;
                }
            }
            PutValue = String.Format(CultureInfo.InvariantCulture, "{0:N2}", ConNum);//格式化为字符串千位符、四舍五入两位小数;
            return PutValue;
        }
        static int[] GetZeroIndexes(string str)
        {
            return str.Select((ch, index) => index)
                      .Where(index => str[index] == '零')
                      .ToArray();
        }
        public static string FormatJudg(string JudgStr) //判断中文大写格式是否符合基本标准
        {
            string[] slarr = new string[] { "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "零" };
            string[] dwarr = new string[] { "垓", "京", "兆", "亿", "万","仟", "佰", "拾", "元", "角", "分", "整", "正" };
            bool JudgBool = false;
            bool JudgBool1 = false;
            bool JudgBool2 = false;
            bool JudgBool3 = false;
            bool JudgBool4 = false;
            bool JudgBool5 = false;
            bool JudgBool6 = false;
            bool JudgBool7 = false;
            if (Array.IndexOf(slarr, JudgStr[0].ToString()) == -1)//判断首字是否为数量
            {
                JudgBool =true;
            }
            if (JudgStr[JudgStr.Length - 1] == '正' || JudgStr[JudgStr.Length - 1] == '整' || JudgStr[JudgStr.Length - 1] == '元' || JudgStr[JudgStr.Length - 1] == '角' || JudgStr[JudgStr.Length - 1] == '分')//判断最后一位是否正确
            {
                JudgBool7 = false;
            }
            else 
            {
                JudgBool7 = true;
            }
            int count = 0;
            for (int i = 0; i <5; i++)//判断单位是否重复
            {
                count = Regex.Matches(JudgStr, dwarr[i].ToString ()).Count;
                if (count >= 2) 
                {
                    JudgBool1 = true;
                    break;
                }
            }
            for (int i = 8; i < slarr.Length; i++)
            {
                count = Regex.Matches(JudgStr, dwarr[i].ToString()).Count;
                if (count >= 2)
                {
                    JudgBool2 = true;
                    break;
                }
            }
            for (int i = 0; i < JudgStr.Length; i++)//判断是否有非中文大写字符
            {
                if (Array.IndexOf(slarr, JudgStr[i].ToString()) == -1 && Array.IndexOf(dwarr, JudgStr[i].ToString()) == -1)
                {
                    JudgBool3 = true;
                    break;
                }
            }
            for (int i = 0; i <= JudgStr.Length-2; i++)//数字后面是否有单位
            {
                if (JudgStr[i] != '零') 
                { 
                   if (Array.IndexOf(slarr, JudgStr[i].ToString()) != -1 && Array.IndexOf(dwarr, JudgStr[i+1].ToString()) == -1)
                   {
                    JudgBool4 = true;
                    break;
                    }
                }    
            }
            int[] uparr= GetZeroIndexes(JudgStr);//判断零后面是否为数字
            for (int i = 0; i < uparr.LongLength-1; i++) 
            {
                if (Array.IndexOf(slarr, JudgStr[uparr[i]+1].ToString()) == -1)
                {
                    JudgBool5= true;
                    break;
                }
            }
            if (JudgStr.Length > 4) 
            {
                    if (JudgStr.IndexOf("元") == -1 && JudgStr.IndexOf("圆") == -1)
                    {
                        JudgBool6 = true;
                    }
            }
         
            if (JudgBool == true || JudgBool1 == true || JudgBool2 == true || JudgBool3 == true || JudgBool4 == true || JudgBool5 == true || JudgBool6 == true || JudgBool7 == true)
            {
                JudgStr = "1";
                return JudgStr;
            }
            else
            {
                JudgStr = "0";
                return JudgStr;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox1.Text = textBox1.Text.Replace("万万", "亿");
            textBox1.Text = textBox1.Text.Replace("万亿", "兆");
            textBox1.Text = textBox1.Text.Replace("万兆", "京");
            textBox1.Text = textBox1.Text.Replace("万京", "垓");
            textBox1.Text = textBox1.Text.Replace("圆", "元");
            textBox1.Text = Regex.Replace(textBox1.Text, @"[^壹贰叁肆伍陆柒捌玖零垓京兆亿万仟佰拾元角分整正]", "");
            if (textBox1.Text == "")
            {
                MessageBox.Show("空值不能转写,请正确输入!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
                return;
            }
            if (FormatJudg(textBox1.Text) =="1")
            {
                MessageBox.Show("中文大写格式不匹配,请正确输入!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
                return;
            }
            textBox2.Text =  RMBTranNum(textBox1.Text);
        }  
    }
}

代码本人已经实测,转换没有发现问题,但在中文转大写转数字时的中文大写格式判定上还须完善。能力水平有限,欢迎大家指导。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!要将人民币金额转换大写金额,您可以使用以下的 C# 代码示例: ```csharp using System; class Program { static string[] chineseDigits = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; static string[] chineseUnits = new string[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; static string ConvertToChinese(double amount) { string amountStr = amount.ToString("F2"); // 将金额保留两位小数,转换为字符串 string[] parts = amountStr.Split('.'); // 拆分整数部分和小数部分 string chineseInteger = ConvertIntegerToChinese(parts[0]); // 转换整数部分 string chineseDecimal = ConvertDecimalToChinese(parts[1]); // 转换小数部分 if (string.IsNullOrEmpty(chineseDecimal)) { return chineseInteger + "元整"; } else { return chineseInteger + "元" + chineseDecimal; } } static string ConvertIntegerToChinese(string intPart) { string result = ""; int length = intPart.Length; bool isZero = true; // 是否需要补零 for (int i = 0; i < length; i++) { int digit = int.Parse(intPart[i].ToString()); int unitPos = length - i - 1; string digitStr = chineseDigits[digit]; if (digit != 0) { isZero = false; result += digitStr + chineseUnits[unitPos]; } else { if (unitPos == 4 && !isZero) // 处理万位的零 { result += digitStr; isZero = true; } else if (unitPos == 8) // 处理亿位的零 { result += digitStr; isZero = true; } } } return result; } static string ConvertDecimalToChinese(string decimalPart) { string result = ""; int length = decimalPart.Length; for (int i = 0; i < length; i++) { int digit = int.Parse(decimalPart[i].ToString()); if (digit != 0) { result += chineseDigits[digit] + chineseUnits[i - length]; } } return result; } static void Main(string[] args) { double amount = 123456.78; // 替换为您要转换金额 string chineseAmount = ConvertToChinese(amount); Console.WriteLine(chineseAmount); } } ``` 以上代码将金额 `123456.78` 转换大写金额 `壹拾贰万叁仟肆佰伍拾陆元柒角捌分`。 您可以在 `Main` 方法中修改 `amount` 变量的值,以转换不同的金额。 希望这能帮到您!如果您有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

happylshk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值