C#实现人民币小写到大写的转换

本文介绍了一款用于将人民币小写金额转换为大写金额的工具。该工具能够处理最多9位数的小写金额,并将其转换为标准的大写格式,适用于财务报表等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常有的一些报表程序需要打印人民币的大写金额,所以写了一个9位之内的人民币小写向大写转换的工具。

代码如下:

using System;
using System.Text;
namespace RMBLowerToUpper
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class LToU
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
//   if(args.Length!=1)
//   {
//    Console.WriteLine("error number!");
//    return;
//   }
   
   string number=null;
   
   do
   {
    Console.WriteLine("Please input a number:");
    StringBuilder sb = new StringBuilder();
    number=Console.ReadLine();
    string[] div=number.Split('.');
    if(div.Length!=2 || div[1].Length !=2)
    {
     Console.WriteLine("error number!");
        return;
    }
//    foreach(string n in div)
//    {
//     Console.WriteLine(n);
//    }
    #region--------处理整数部分-------------
    char[] bit=div[0].ToCharArray();
    //先转换成大写
    for(int i=0;i<bit.Length ;i++)
    {
     switch(bit[i])
     {
      case '1':
       sb.Append("壹");
       break;
      case '2':
       sb.Append ("贰");
       break;
      case '3':
       sb.Append("叁");
       break;
      case '4':
       sb.Append ("肆");
       break;
      case '5':
       sb.Append("伍");
       break;
      case '6':
       sb.Append ("陆");
       break;
      case '7':
       sb.Append("柒");
       break;
      case '8':
       sb.Append ("捌");
       break;
      case '9':
       sb.Append("玖");
       break;
       //处理诸如10000.00,10020.00,1002000.00的情况
      case '0':
       if((i+1)<bit.Length  && bit[i+1]=='0')
       {
        sb.Append ("");
       }
       else if(i==bit.Length -1 || bit.Length -i==5 || bit.Length -i==9)
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("零");
       }
       break;
     

     }
     //插入相应的分割单位
     switch(bit.Length -i)
     {
      case 2:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("拾");
       }
       break;
      case 3:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("百");
       }
       break;
      case 4:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("仟");
       }
       break;
      case 5:
//       if(bit[i]=='0')
//       {
//        sb.Append ("");
//       }
//       else
//       {
        sb.Append ("万");
//       }
       break;
      case 6:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("拾");
       }
       break;
      case 7:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("百");
       }
       break;
      case 8:
       if(bit[i]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("仟");
       }
       break;
      

     }
    }
    sb.Append ("圆整");
    #endregion

    #region ----------处理小数部分-----------
    char[] point=div[1].ToCharArray();
    for(int j=0;j<point.Length ;j++)
    {
     switch(point[j])
     {
      case '1':
       sb.Append("壹");
       break;
      case '2':
       sb.Append ("贰");
       break;
      case '3':
       sb.Append("叁");
       break;
      case '4':
       sb.Append ("肆");
       break;
      case '5':
       sb.Append("伍");
       break;
      case '6':
       sb.Append ("陆");
       break;
      case '7':
       sb.Append("柒");
       break;
      case '8':
       sb.Append ("捌");
       break;
      case '9':
       sb.Append("玖");
       break;
      case '0':
       if((j+1)<point.Length  && point[j+1]=='0')
       {
        sb.Append ("");
       }
       else if(j==point.Length -1)
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("零");
       }
       break;
     

     }
     switch(point.Length -j)
     {
      case 2:
       if(point[j]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("角");
       }
       break;
      case 1:
       if(point[j]=='0')
       {
        sb.Append ("");
       }
       else
       {
        sb.Append ("分");
       }
       break;
     }
    }
    #endregion
    string u=sb.ToString();
    Console.WriteLine(u);
   }while(number.ToUpper()!="EXIT");

  }
 }
}

测试界面如下:

 测试界面

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值