当前位置: 首页 > 编程日记 > 正文

人民币大小写转换

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace HKH.Common
{
 /// <summary>
 /// 人民币大小写格式转换
 /// </summary>
 /// <remarks> Create By Lwt on 2006/09/23
 /// </remarks>
 public class clsRMB
 {
  private clsRMB()
  {
  }

#region 格式化

/// <summary>
  /// 格式化(大写转小写)
  /// </summary>
  /// <param name="strRMB"></param>
  /// <returns></returns>
  public static double Format(string strRMB)
  {
   try
   {
    //正则表达式,验证第一位是否阿拉伯数字,确定转换格式
    //1.5亿----混写格式
    if(Regex.IsMatch(strRMB,"^//d"))
    {
     //去掉元单位
     strRMB = Regex.Replace(strRMB,"元|圆","");
     char temp = strRMB[strRMB.Length - 1];
     if (temp == '万' || temp == '萬' || temp == '亿')
     {
      return Convert.ToDouble(strRMB.Substring(0,strRMB.Length - 1)) * Math.Pow(10,GetExp(temp));
     }
     else
     {
      return Convert.ToDouble(strRMB);
     }
    }
    //壹亿伍千万-----大写格式
    else 
    {
     return Eval(strRMB);
    }

}
   catch
   {
    return -1;
   }
  }

/// <summary>
  /// 格式化(小写转大写)
  /// </summary>
  /// <param name="numRMB"></param>
  /// <returns></returns>
  public static string Format(double numRMB)
  {
   try
   {
    if( 0 == numRMB)
     return "零元整";

StringBuilder szRMB = new StringBuilder();

//乘100以格式成整型,便于处理
    ulong iRMB = Convert.ToUInt64(numRMB * 100);

szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 100),-2));
  
    //去掉原来的小数位
    iRMB = iRMB / 100;

int iUnit = 0;

//以每4位为一个单位段进行处理,所以下边除以10000
    while(iRMB != 0)
    {
     szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 10000),iUnit ));
     iRMB = iRMB / 10000;
     iUnit += 4;
    }
      
    string strRMB = szRMB.ToString();
    
    //格式修正
    strRMB = Regex.Replace(strRMB,"零+","零");
    strRMB = strRMB.Replace("元零整","元整");
    strRMB = strRMB.Replace("零元","元");

return strRMB.Trim('零');
   }
   catch
   {
    return "";
   }
  }

#endregion

#region 私有方法

/// <summary>
  /// 计算表达式(大写表达式求值)
  /// </summary>
  /// <param name="strRMB"></param>
  /// <returns></returns>
  private static double Eval(string strRMB)
  {
   try
   {
    if (null == strRMB )
     return 0;

strRMB = Replace(strRMB,false);

if ("" == strRMB)
     return 0;

#region 利用位权进行计算

//基础指数
    int basicExp = 0;
    //当前指数
    int currExp = 0;

double numRMB = 0;

for(int i = strRMB.Length - 1; i > -1 ; i --)
    {
     char temp = strRMB[i];

if (temp == '元' || temp == '万' || temp == '亿' || temp == '圆' || temp == '萬' )
     {
      basicExp = GetExp(temp);
      currExp = 0;

continue;
     }
     else
     {
      if(Regex.IsMatch(temp.ToString(),"^//d"))
      {
       numRMB = numRMB + Convert.ToInt32(temp.ToString()) * Math.Pow(10,(basicExp + currExp));
      }
      else
      {
       currExp = GetExp(temp);
      }

}
    }

#endregion

return numRMB;
   }
   catch
   {
    return -1;
   }
  }

/// <summary>
  /// 计算表达式(小写数值求大写字符串)
  /// </summary>
  /// <param name="numRMB"></param>
  /// <param name="iUnit"></param>
  /// <returns></returns>
  private static string ToUpper(int numRMB,int iUnit)
  {
   try
   {
    if( 0 == numRMB )
    {
     if (iUnit == -2)
     {
      return "整";
     }
     
     if (iUnit == 0)
     {
      return "元";
     }

return "零";
    }

StringBuilder szRMB = new StringBuilder();

string strRMB = "";
    
    #region 对角/分做特殊处理

if (iUnit == -2)
    {
     int jiao = numRMB / 10;
     int fen = numRMB % 10;

if (jiao > 0)
     {
      szRMB.Append(jiao);
      szRMB.Append(GetUnit(-1));

if ( fen > 0)
      {
       szRMB.Append(fen);
       szRMB.Append(GetUnit(-2));
      }
     }
     else
     {
      szRMB.Append(fen);
      szRMB.Append(GetUnit(-2));
     }

return Replace(szRMB.ToString(),true);
    }

#endregion

#region 以下为整数部分正常处理

strRMB = numRMB.ToString("0000");

//前一位是否是0
    bool hasZero = false;
    
    for ( int i = 0; i < strRMB.Length; i++ )
    {
     //只有四位,最高位为‘千’,所以下边的3-i为单位修正
     if ( ( 3-i ) > 0)
     {
      if( '0' != strRMB[i] )
      {
       szRMB.Append(strRMB[i]);
       szRMB.Append(GetUnit( 3-i ));
       hasZero = false;
      }
      else
      {
       if( !hasZero )
        szRMB.Append(strRMB[i]);

hasZero = true;
      }
     }
     //最后一位,特别格式处理
     //如最后一位是零,则单位应在零之前
     else
     {
      if( '0' != strRMB[i] )
      {
       szRMB.Append(strRMB[i]);
       szRMB.Append(GetUnit( iUnit ));
       hasZero = false;
      }
      else
      {
       if (hasZero)
       {
        szRMB.Insert(szRMB.Length - 1,GetUnit( iUnit ));
       }
       else
       {
        szRMB.Append(GetUnit( iUnit ));
        szRMB.Append(strRMB[i]);
       }
      }
     }
    }

//转换大写后返回
    return Replace(szRMB.ToString(),true);

#endregion
   }
   catch
   {
    return "";
   }
  }

/// <summary>
  /// 将中文大写换成阿拉伯数字
  /// </summary>
  /// <param name="strRMB"></param>
  /// <param name="toUpper">true--转换为大写/false--转换为小写</param>
  /// <returns></returns>
  private static string Replace(string strRMB,bool toUpper)
  {
   if(toUpper)
   {
    strRMB = strRMB.Replace("0","零");
    strRMB = strRMB.Replace("1","壹");
    strRMB = strRMB.Replace("2","贰");
    strRMB = strRMB.Replace("3","叁");
    strRMB = strRMB.Replace("4","肆");
    strRMB = strRMB.Replace("5","伍");
    strRMB = strRMB.Replace("6","陆");
    strRMB = strRMB.Replace("7","柒");
    strRMB = strRMB.Replace("8","捌");
    strRMB = strRMB.Replace("9","玖");
   }
   else
   {
    strRMB = strRMB.Replace("零","0");
    strRMB = strRMB.Replace("壹","1");
    strRMB = strRMB.Replace("贰","2");
    strRMB = strRMB.Replace("叁","3");
    strRMB = strRMB.Replace("肆","4");
    strRMB = strRMB.Replace("伍","5");
    strRMB = strRMB.Replace("陆","6");
    strRMB = strRMB.Replace("柒","7");
    strRMB = strRMB.Replace("捌","8");
    strRMB = strRMB.Replace("玖","9");
   }
   return strRMB;
  }

/// <summary>
  /// 获取单位名称
  /// </summary>
  /// <param name="iCode"></param>
  /// <returns></returns>
  private static string GetUnit(int iCode)
  {
   switch(iCode)
   {
    case -2:
     return "分";
    case -1:
     return "角";
    case 0:
     return "元";
    case 1:
     return "拾";
    case 2:
     return "佰";
    case 3:
     return "仟";
    case 4:
     return "萬";
    case 8:
     return "亿";
    default:
     return "";
   }
  }

/// <summary>
  /// 获取位权指数
  /// </summary>
  /// <param name="cUnit"></param>
  /// <returns></returns>
  private static int GetExp(char cUnit )
  {
   switch(cUnit)
   {
    case '分':
     return -2;
    case '角':
     return -1;
    case '元':
    case '圆':
     return 0;
    case '十':
    case '拾':
     return 1;
    case '百':
    case '佰':
     return 2;
    case '千':
    case '仟':
     return 3;
    case '万':
    case '萬':
     return 4;
    case '亿':
     return 8;
    default:
     return 0;
   }
  }

#endregion

}
}


相关文章:

冒泡排序(java实现)

冒泡排序&#xff0c;就是每次遍历都会把最小(或者最大)的数放在前面。比如要升序{A1,........An} 第一次排序要取出整个数组中最小放在A1的位置&#xff0c;从An开始往前遍历&#xff0c;相邻两个数比较&#xff0c;如果Aj < Aj-1 则换位。知道比较到A1 这一趟完事之后 A…

好看又好用的 GUI,你需要这七个 Python 必备库,

来源 | 法纳斯特头图 | 下载于ICphotoGUI(图形用户界面)&#xff0c;顾名思义就是用图形的方式&#xff0c;来显示计算机操作的界面&#xff0c;更加方便且直观。与之相对应的则是CUI(命令行用户交互)&#xff0c;就是常见的Dos命令行操作&#xff0c;需要记忆一些常用的命令&a…

总结PHP 7新增加的特性

?? 运算符&#xff08;NULL 合并运算符&#xff09; 把这个放在第一个说是因为我觉得它很有用。用法&#xff1a; $a $_GET[a] ?? 1;它相当于&#xff1a; <?PHP $a isset($_GET[a]) ? $_GET[a] : 1; 我们知道三元运算符是可以这样用的&#xff1a; $a ?: 1但是这是…

谈“云”色变?近80%企业曾遭受数据泄露

出品 | 《大咖来了》 一边是企业上云这一毋庸置疑的发展趋势&#xff0c;但另一边&#xff0c;云数据泄露事件的频繁&#xff0c;却让不少企业谈“云”色变。 2020年2月&#xff0c;万豪酒店520万客人信息被泄露&#xff0c;英国信息专员办公室(ICO)对其进行了1840万英镑(约1.…

C语言的32个关键字

C语言的关键字共有32个&#xff0c;根据关键字的作用&#xff0c;可分其为数据类型关键字、控制语句关键字、存储类型关键字和其它关键字四类。1 数据类型关键字&#xff08;12个&#xff09;&#xff1a; (1) char &#xff1a;声明字符型变量或函数 (2) double &#xff1a;声…

Python中线程Timeout的使用

Python中关于Timeout有另一种用起来更简便的方法&#xff0c;即使用装饰器。这种方式是使用sys模块的settrace等方法重构了python的threading类&#xff1a;#!/usr/bin/python import threading import sys class KThread(threading.Thread):"""Subclass of thr…

Vue的模板语法学习

模板语法 1、插值 a、文本 数据绑定最常见的形式就是使用 “Mustache” 语法&#xff08;双大括号&#xff09;的文本插值 我们在普通插值的时候无论何时&#xff0c;绑定的数据对象上 msg 属性发生了改变&#xff0c;插值处的内容都会更新 【案例】 <div id"app"…

求二维数组中最大子数组的和

任国庆 张博 之前我们讨论了在一维数组中求最大子数组的和&#xff0c;在此基础上我们开始讨论二维数组的最大子数组。 求二维数组的最大子数组思想是建立在以为数组。首先将数组的第一列看成一个一维数组&#xff0c;找到该列的最大子数组的值&#xff0c;然后将第二列与第一…

赠书 | 详解 4 种爬虫技术

作者 | 赵国生 王健来源 | 大数据DT头图 | 下载于视觉中国前言&#xff1a;聚焦网络爬虫是“面向特定主题需求”的一种爬虫程序&#xff0c;而通用网络爬虫则是捜索引擎抓取系统&#xff08;Baidu、Google、Yahoo等&#xff09;的重要组成部分&#xff0c;主要目的是将互联网上…

nginx 通过proxy_next_upstream实现容灾和重复处理问题

proxy_next_upstream指令语法: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 |http_504 |http_404 | off ...; 默认值: proxy_next_upstream error timeout; 上下文: http, server, locationerror # 和后端服务器建立连接时&…

javascript身份证号码验证函数支持带x

//--身份证号码验证-支持新的带x身份证functionisIdCardNo(num) { varfactorArr newArray(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); varerror; varvarArray newArray(); varintValue; varlngProduct 0; varintCheckDigit; varintStrLen num.length; v…

「AI 质检员」在富士通上岗,效率比人工高 25%

日本第一 IT 厂商富士通&#xff0c;于近日宣布开发了用于检测产品外观异常的 AI 技术&#xff0c;从而节省人力成本、材料成本等&#xff0c;同时也可节省声誉损失和退货/召回相关的成本&#xff0c;「无人工厂」已来。来源 | Hyper超神经责编 | 寇雪芹头图 | 下载于视觉中国去…

asp在线压缩和解压缩文件(文件夹)

asp在线压缩和解压缩文件&#xff08;文件夹&#xff09; <%\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 1. c:\windows\system32\cmd.exe\\ 拷贝把本文件所在的路径\\\\ 2. 把 c:\program\winrar\rar.exe\\ 拷贝把本文件所在的路径 并改名为WinRAR.e…

SpringMVC + Hibernate-Validator 参数校验

2019独角兽企业重金招聘Python工程师标准>>> 前言&#xff1a; Web开发中&#xff0c;最为常见的场景就是前端表单数据、Json数据与后端实体类的绑定&#xff0c;即使JS能校验并阻止大部分的必填漏填的风险&#xff0c;但并不能防止恶意破坏者修改脚本。因此后端参数…

深入浅出,机器学习该怎么入门?

来源 | 算法进阶责编 | 寇雪芹头图 | 下载于视觉中国前言&#xff1a;机器学习作为人工智能领域的核心组成&#xff0c;是计算机程序学习数据经验以优化自身算法&#xff0c;并产生相应的“智能化的”建议与决策的过程。一个经典的机器学习的定义是&#xff1a;A computer prog…

防止SQL注入式攻击

1将sql中使用的一些特殊符号&#xff0c;如 -- /* ; %等用Replace()过滤&#xff1b;2限制文本框输入字符的长度&#xff1b;3检查用户输入的合法性&#xff1b;客户端与服务器端都要执行&#xff0c;可以使用正则。4使用带参数的SQL语句形式。 尽量用存储过程

js中document.write的那点事

document.write()方法可以用在两个方面&#xff1a;页面载入过程中用实时脚本创建页面内容&#xff0c;以及用延时脚本创建本窗口或新窗口的内容。该方法需要一个字符串参数&#xff0c;它是写到窗口或框架中的HTML内容。这些字符串参数可以是变量或值为字符串的表达式&#xf…

SVN提示被锁定的解决方法(转)

1、&#xff08;常用&#xff09;出现这个问题后使用“清理”即"Clean up"功能&#xff0c;如果还不行&#xff0c;就直接到上一级目录&#xff0c;再执行“清理”&#xff0c;然后再“更新”。 2、&#xff08;没试过&#xff09;有时候如果看到某个包里面的文件夹没…

征集佳句-精妙SQL语句收集

征集佳句-精妙SQL语句收集 SQL语句先前写的时候&#xff0c;很容易把一些特殊的用法忘记&#xff0c;我特此整理了一下SQL语句操作&#xff0c;方便自己写SQL时方便一点&#xff0c;想贴上来&#xff0c;一起看看&#xff0c;同时希望大家能共同多多提意见&#xff0c;也给我…

【WP8】ResourceDictionary

WP8中引用资源字典 当我们定义的样式太多的时候&#xff0c;我们可以把样式分别定义在不同的文件中&#xff0c;然后通过 MergedDictionaries 应用到其他资源字典中&#xff0c;看下面Demo 我们可以把样式定义在多个文件中&#xff0c;然后再App.xaml中引用 我们先定义三个文件…

拿来就能用! CTO 创业技术栈指南!

作者 | Nitin Aggarwal译者 | 弯月出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;随着开发运维概念的诞生&#xff0c;以及“You build it, you run it.”&#xff08;谁构建&#xff0c;谁运维&#xff09;理念的盛行&#xff0c;现代创业公司的技术栈也发生了许…

Go处理百万每分钟的请求

2019独角兽企业重金招聘Python工程师标准>>> I have been working in the anti-spam, anti-virus and anti-malware industry for over 15 years at a few different companies, and now I know how complex these systems could end up being due to the massive a…

data pump工具

expdp和impdp的用法ORCALE10G提供了新的导入导出工具&#xff0c;数据泵。Oracle官方对此的形容是&#xff1a;OracleDataPump technology enables Very High-Speed movement of data and metadata from one database to another.其中Very High-Speed是亮点。先说数据泵提供的主…

游标对于分页存储过程

1。我个人认为最好的分页方法是: Select top 10 * from table where id>200写成存储过程,上面的语句要拼一下sql语句,要获得最后大于的哪一个ID号 2。那个用游标的方式,只适合于小数据量的表,如果表在一万行以上,就差劲了 你的存储过程还比不上NOT IN分页,示例: SELECT …

混沌、无序、变局?探索之中,《拟合》开启

从无序中寻找踪迹&#xff0c;从眼前事探索未来在东方的神话里&#xff0c;喜鹊会搭桥&#xff0c;银河是条河&#xff0c;两端闪耀的牵牛星和织女星&#xff0c;则是一年一相会的佳偶&#xff0c;他们彼此间的惦念&#xff0c;会牵引彼此在七夕那天实现双星聚首。在西方的世界…

linxu 下安装mysql5.7.19

2019独角兽企业重金招聘Python工程师标准>>> 1、首先检查是否已经安装过mysql,查找mysql相关软件rpm包 # rpm -qa | grep mysql 2、将所有与mysql相关的东西删除 #yum -y remove mysql-libs-5.1.66-2.el6_3.x86_64 3、安装依赖包 #yum -y install make gcc-c cmake …

C#技术内幕 学习笔记

引用类型是类型安全的指针&#xff0c;它们的内存是分配在堆&#xff08;保存指针地址&#xff09;上的。String、数组、类、接口和委托都是引用类型。 强制类型转换与as类型转换的区别&#xff1a;当类型转换非法时&#xff0c;强制类型转换将抛出一个System.InvalidCastExce…

java的深度克隆

原文&#xff1a;http://blog.csdn.net/randyjiawenjie/article/details/7563323javaobjectinterfacestringclassexception先做个标记 http://www.iteye.com/topic/182772 http://www.blogjava.net/jerry-zhaoj/archive/2009/10/14/298141.html 关于super.clone的理解 http://h…

持续推进预估时间问题研究,滴滴盖亚计划开放ETA数据集

4月29日消息&#xff0c;为持续推进行程时长预估问题研究&#xff0c;滴滴联合GIS(地理信息系统)领域国际顶会ACM SIGSPATIAL发布ACM SIGSPATIAL GISCUP 2021比赛&#xff0c;鼓励研究者们基于滴滴新开放的行程时长数据集&#xff0c;进一步提升时间预估准确性。 预估到达时间…

3.Java集合-HashSet实现原理及源码分析

一、HashSet概述&#xff1a; HashSet实现Set接口&#xff0c;由哈希表&#xff08;实际上是一个HashMap实例&#xff09;支持&#xff0c;它不保证set的迭代顺序很久不变。此类允许使用null元素 二、HashSet的实现&#xff1a; 对于HashSet而言&#xff0c;它是基于HashMap实现…