源代码:
<%@ Page language="c#" %>
<%@ import NameSpace="System.Security.Cryptography" %><html>
<head>
<title>TripleDES(3DES)加密器</title>
<script language="C#" runat="server">
private void Page_Load(Object sender,EventArgs e)
{
  Response.Write(ReadRMB("7654321.5"));
}//将阿拉伯数字表示的人民币金额转换成大写的汉字金额。注;阿拉佰数字的整数和小数位分别不得超过16位
public string ReadRMB(string numRMB)
{
  //string numRMB=strNum;
  string[] arrCNNumber={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
  string[] arrUnit={"仟万亿","佰万亿","拾万亿","万亿","仟亿","佰亿","拾亿","亿","仟万","佰万","拾万","万","仟","佰","拾",""};
  int index=numRMB.IndexOf('.');
  string numPart1=numRMB.Substring(0,index);  //取整数部分
  string numPart2=numRMB.Substring(index+1,numRMB.Length-index-1);  //取小数部分
  string CNRMB1="",CNRMB2=""; //大写汉字金额整数和小数部分
  string unit=" ";
  for(int i=0;i<numPart1.Length;i++)
  {
    unit=arrUnit[arrUnit.Length-numPart1.Length+i]; //单位    
    if(numPart1.Substring(i,1)=="0") //若当前位为0
      {
          if(CNRMB1.Substring(CNRMB1.Length-1,1)=="零") //若前一位也为0,则不读当前位
          {
              if(unit.Substring(0,unit.Length-1)=="") //若当前位是是单位位,则需加上单位
              {
                CNRMB1+=(""+unit);
              }
              else
              {
                CNRMB1+="";
              }
          }
          else //前一位不为0
          {
              if(unit.Substring(0,unit.Length-1)=="") //若当前位是是单位位,则直接转为单位,否则转为“零”
              {
                CNRMB1+=unit;
              }
              else
              {
                CNRMB1+="零";
              }
          }
      }
      
    else
      {
           int n=Convert.ToInt32(numPart1.Substring(i,1));
           //若当前位不是处在仟、万、亿等单位位的位置上(如拾万、佰万、仟亿等),则须省掉末尾的单位(仟、万、亿)
           if(unit.Length==1)
           {
             CNRMB1+=(arrCNNumber[n]+unit);         
           }
           else
           {
             unit=unit.Substring(0,unit.Length-1);
             CNRMB1+=(arrCNNumber[n]+unit);
           }
           
       }
       
  }
  
  //小数部分的转换还未实现
  return CNRMB1;
}
</script>
</head>
<body></body>
</html>错误症状;
Server Error in '/' Application.
--------------------------------------------------------------------------------Length cannot be less than zero.
Parameter name: length 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: lengthSource Error: 
Line 61:            else
Line 62:            {
Line 63:              unit=unit.Substring(0,unit.Length-1);
Line 64:              CNRMB1+=(arrCNNumber[n]+unit);
Line 65:            }
 Source File: c:\Inetpub\wwwroot\RMB.aspx    Line: 63 Stack Trace: 
[ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
   System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +2848721
   System.String.Substring(Int32 startIndex, Int32 length) +11
   ASP.rmb_aspx.ReadRMB(String numRMB) in c:\Inetpub\wwwroot\RMB.aspx:63
   ASP.rmb_aspx.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\RMB.aspx:10
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061 
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 

解决方案 »

  1.   

    unit.Length-1 < 0, 自己调试一下吧。
      

  2.   

    >>>>>>>>
    呆呆虫
    unit.Length-1  < 0, 自己调试一下吧。------------------------------
    我也知道这unit.Length-1  < 0啊,但就是不知道怎么会这样?
      

  3.   

     string[] arrUnit={"仟万亿","佰万亿","拾万亿","万亿","仟亿","佰亿","拾亿","亿","仟万","佰万","拾万","万","仟","佰","拾",""}; 哦,原来是当最后到最后一个""字串时Length的值为0!