将阿拉伯数字转成中文发票写法。

最近帮HR部门做一个文档批量生成系统,其中有个特殊的需求,就是在生成劳动合同的时候需要将阿拉伯数字转成中文发票写法,经过努力,略有小成,现留下代码以便查用:

/// <summary>
  /// Determines whether the string is a number string.
  /// </summary>
  /// <param name="ArabiaNumbers">An arabia number string.</param>
  /// <returns>
  ///If the parameter is an arabia number string, return <code>true</code> , otherwise <code>false</code>.
  ///</returns>
  bool IsNumber(string ArabiaNumbers)
  {
   bool result = false;
   if(ArabiaNumbers != null && ArabiaNumbers.Length != 0)
   {    
    string[] parts = ArabiaNumbers.Split('.');
    string integerPart = string.Empty;
    string decimalFractionPart = string.Empty;
    if(parts.Length == 2)
    {
     integerPart = parts[0];
     decimalFractionPart = parts[1];
    }
    else
    {
     integerPart = parts[0];
    }

    char[] everyInteger = integerPart.ToCharArray();
    for(int i = 0; i < everyInteger.Length; i++)
    {
     if(everyInteger[i] > (int)'0' && everyInteger[i] < (int)'9')
      continue;
     else
     {
      result = false;
      break;
     }
    }

    if(decimalFractionPart != string.Empty)
    {
     char[] everyDecimalFraction = decimalFractionPart.ToCharArray();
     for(int i = 0; i < everyDecimalFraction.Length; i++)
     {
      if(everyDecimalFraction[i] > (int)'0' && everyDecimalFraction[i] < (int)'9')
       continue;
      else
      {
       result = false;
       break;
      }
     }
    }
    result = true;
   }
   return result;
  }

/// <summary>
  /// Converts arabia number to chinese.
  /// </summary>
  /// <param name="ArabiaNumber">Arabia number</param>
  /// <returns>
  /// The chinese word according to the arabia number.
  /// </returns>
  private string ToChineseNumber(char ArabiaNumber)
  {
   switch(ArabiaNumber)
   {
    case '0':
     return "零";
    case '1':
     return "壹";
    case '2':
     return "贰";
    case '3':
     return "叁";
    case '4':
     return "肆";
    case '5':
     return "伍";
    case '6':
     return "陆";
    case '7':
     return "柒";
    case '8':
     return "捌";
    case '9':
     return "玖";
    default:
     return string.Empty;
   }
  }

/// <summary>
  /// Gets the number's exponent.
  /// </summary>
  /// <param name="position">The number's position.</param>
  /// <returns>The number's exponent.</returns>
  private string GetExponent(int position)
  {
   switch(position)
   {
    case 2:
     return "拾";
    case 3:
     return "佰";
    case 4:
     return "仟"; 
    case 5:
     return "万";
    case 9:
     return "亿";
    default:
     break;
   }
   if(position > 5 && position < 9)
   {
    return GetExponent(position - 4);
   }
   if(position > 9)
   {
    return GetExponent(position - 8);
   }
   
   return string.Empty;
  }

/// <summary>
  /// Convert an arabia number to chinese.
  /// </summary>
  /// <param name="ArabiaNumber">Arabia number.</param>
  /// <returns>Chinese according to the arabia number.</returns>
  private string ConvertToChinese(string ArabiaNumber)
  {   
   //If the parameter is null or an empty string, return null.
   if(ArabiaNumber == null || ArabiaNumber.Length == 0)
   {
    return null;
   }

   //Check if the parameter is a number string.
   //If yes, do convertion.else return null.
   if(IsNumber(ArabiaNumber))
   {
    string IntegerPart = string.Empty;
    string DecimalFractionPart = string.Empty;
    StringBuilder ChineseIntegerPart = new StringBuilder();
    StringBuilder ChineseDecimalFractionPart = new StringBuilder();

    string[] temp = ArabiaNumber.Split('.');
    //there are both integer part and decimal fraction part.
    if(temp.Length == 2)
    {
     if(temp[0].Length != 1)
     {
      IntegerPart = temp[0].Trim().TrimStart('0');
     }
     else
     {
      IntegerPart = temp[0].Trim();
     }
     DecimalFractionPart = temp[1].Trim();
    }
    //There is only integer part.
    else
    {
     if(temp[0].Length != 1)
     {
      IntegerPart = temp[0].Trim().TrimStart('0');
     }
     else
     {
      IntegerPart = temp[0].Trim();
     }
    }
    
    char[] EveryInteger = IntegerPart.ToCharArray();
    //There is only one number in integer part.
    if(EveryInteger.Length == 1)
    {
     if(EveryInteger[0] == '0')
      ChineseIntegerPart.Append(this.ToChineseNumber(EveryInteger[0]));
     else
      ChineseIntegerPart.Append(this.ToChineseNumber(EveryInteger[0]));
    }
    //There are not only one number in integer part.
    else
    {
     int countOfZero = 0;
     for(int i = 0; i <= EveryInteger.Length - 1; i++)
     {
      if(EveryInteger[i] == '0')
      {
       if(EveryInteger.Length - i == 9)
       {
        ChineseIntegerPart.Append(this.GetExponent(9));
       }
       if(EveryInteger.Length - i == 5)
       {
        ChineseIntegerPart.Append(this.GetExponent(5));
       }
       countOfZero++;
      }
      else
      {
       //There are more than one zeroes sequently, convert only one time.
       if(countOfZero >= 1)
       {
        //There are zeroes till the end, don't convert.
        if(i == EveryInteger.Length - 1)
        {
         break;
        }
        else
        {
         //When meeting 9th number, add an exponent extraly..
         if(EveryInteger.Length - i == 9)
         {
          ChineseIntegerPart.Append(this.GetExponent(9));
         }
         else
         {
          //When meeting 5th number, add an exponent extraly.
          if(EveryInteger.Length - i == 5)
          {
           ChineseIntegerPart.Append(this.GetExponent(5));
          }
          else
          {
           ChineseIntegerPart.Append(this.ToChineseNumber('0'));
           countOfZero = 0;
           ChineseIntegerPart.Append(this.ToChineseNumber(EveryInteger[i]));
           ChineseIntegerPart.Append(this.GetExponent(EveryInteger.Length - i));
          }
         }
        }
       }
       else
       {
        ChineseIntegerPart.Append(this.ToChineseNumber(EveryInteger[i]));
        ChineseIntegerPart.Append(this.GetExponent(EveryInteger.Length - i));
       }
      }
     }    
    }
    //If decimal fraction part exist, convert them.
    if(DecimalFractionPart != string.Empty)
    {
     ChineseDecimalFractionPart.Append("点");
     char[] EveryDecimalFraction = DecimalFractionPart.ToCharArray();
     for(int i = 0; i <= EveryDecimalFraction.Length - 1; i ++)
     {
      ChineseDecimalFractionPart.Append(this.ToChineseNumber(EveryDecimalFraction[i]));
     }
    }

    return ChineseIntegerPart.ToString() + ChineseDecimalFractionPart.ToString();
   }
   else
   {
    return null;
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值