数字的汉字

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

namespace Price
{
  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);

 if(strPCget.LastIndexOf("元")==strPCget.Length-1)
                    {
                        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;
  }
  }
  }
}
 

你可能感兴趣的:(职场,Class,汉字,public,休闲)