http://www.fairage.com/common/courseinfor.me?FuncID=courseinfor&course_id=372

解决方案 »

  1.   

    /**
             * 小写金额转换为大写金额
             * <p>
             * 注意事项:
             * <UL>
             * <LI>返回的大写金额自动加上了货币单位(固定为: '元'、'角'、'分')
             * <LI>适用区间:0.01 <=目标数字 <100,000,000,000,000.00
             * </UL>
             * <p>
             * 调用例子:
             *
             * <pre>
             * strNoCaption = UtFormat.toCaption(dObjNumber); //其中dObjNumber为需要转换的数字
             *
             * </pre>
             *
             * @param dObjNumber
             *            待转换金额
             * @return 大写金额字符串
             */
            public static String toCaption(double dObjNumber) {
                    String[] straNum = {
                              "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
                    String[] straBit = {
                              "仟", "佰", "拾", "万", "仟", "佰", "拾", "亿", "仟", "佰",
                              "拾", "万", "仟", "佰", "拾", "元", "角", "分"};
                    String[] straTmp = new String[40];
                    String[] straObjNum = new String[40];
                    double dTmpQuotient = 0;
                    int[] naObjNum = new int[20];
                    int nCounterI = 0;
                    int nCounterJ = 0;
                    int nCounterM = 0;
                    int nObjLen = 0;
                    double dDeno = dDenominator; //2002/3/19Leixin: 代替dDenominator变量
                    StringBuffer strbTmp = new StringBuffer("");                /** 私有大写数字变量 */
                    String strNoCaption;                if (dObjNumber == 0) {
                            //若目标数字等于零
                            strNoCaption = "零元整";
                    }
                    else { //若目标数字不等于零
                            SSTrace.debug("UtFormat", "toCaption()", "dObjNumber != 0");
                            dObjNumber = dObjNumber * 100; //消除小数点                        while (dDeno >= 1) { //逐步降低分母值
                                    dTmpQuotient = dObjNumber / dDeno;
                                    if (dTmpQuotient >= 1) {
                                            break;
                                    }
                                    dDeno = dDeno / 10;
                            }
                            nCounterI = 0;
                            while (dDeno >= 1) { //拆分数字,存为数组
                                    dTmpQuotient = dObjNumber / dDeno; //取商
                                    naObjNum[nCounterI] = (int) (dTmpQuotient);
                                    dObjNumber = dObjNumber % dDeno;
                                    dDeno = dDeno / 10;
                                    nCounterI++;
                            }
                            nObjLen = nCounterI;
                            nCounterJ = (nObjLen * 2 - 1);
                            nCounterM = 17;
                            for (nCounterI = nObjLen - 1; nCounterI >= 0; nCounterI--) { //从最后一个数字开始循环
                                    straTmp[nCounterJ] = straBit[nCounterM];
                                    nCounterJ--;
                                    straTmp[nCounterJ] = straNum[naObjNum[nCounterI]]; //替换为大写
                                    nCounterJ--;
                                    nCounterM--;
                            }
                            nObjLen = nObjLen * 2;
                            nCounterJ = 0;
                            nCounterM = 0;
                            SSTrace.debug("UtFormat", "toCaption()", "nObjLen=" + nObjLen);
                            for (nCounterI = 0; nCounterI < nObjLen; nCounterI++) { //删除多余的字符
                                    SSTrace.debug("UtFormat", "toCaption()", "nCounterI="
                                                  + nCounterI + "straTmp[nCounterI]="
                                                  + straTmp[nCounterI]);
                                    if (straTmp[nCounterI].compareTo("零") == 0) {
                                            SSTrace.debug("UtFormat", "toCaption()",
                                                      "straTmp[nCounterI+1]=" + straTmp[nCounterI + 1]);
                                            if (straTmp[nCounterI + 1] != null) {
                                                    SSTrace.debug("UtFormat", "toCaption()",
                                                              "straTmp[nCounterI+1]="
                                                              + straTmp[nCounterI + 1]);
                                                    if ( (straTmp[nCounterI + 1].compareTo("万") != 0)
                                                              && (straTmp[nCounterI + 1].compareTo("亿") != 0)
                                                              && (straTmp[nCounterI + 1].compareTo("元") != 0)) {
                                                            straObjNum[nCounterJ] = straTmp[nCounterI];
                                                            SSTrace.debug("UtFormat", "toCaption()",
                                                                      "straObjNum[nCounterJ]="
                                                                      + straObjNum[nCounterJ]
                                                                      + ", straTmp[nCounterI]="
                                                                      + straTmp[nCounterI]);
                                                            nCounterI++;
                                                            nCounterJ++;
                                                            continue;
                                                    }
                                                    else {
                                                            nCounterI++; //保留"亿","万","元"
                                                            SSTrace.debug("UtFormat", "toCaption()",
                                                                      "nCounterI++=" + nCounterI);
                                                            straObjNum[nCounterJ] = straTmp[nCounterI];
                                                            SSTrace.debug("UtFormat", "toCaption()",
                                                                      "straObjNum[nCounterJ]="
                                                                      + straObjNum[nCounterJ]
                                                                      + ", straTmp[nCounterI]="
                                                                      + straTmp[nCounterI]);
                                                            nCounterJ++;
                                                            continue;
                                                    }
                                            }
                                    }
                                    else {
                                            straObjNum[nCounterJ] = straTmp[nCounterI];
                                            nCounterJ++;
                                    }
                            }
      

  2.   

    nCounterJ = 0;
                            SSTrace.debug("UtFormat", "toCaption()", "nCounterJ=" + nCounterJ);
                            while (straObjNum[nCounterJ] != null) { //消除"亿","万","元"前的"零"字
                                    SSTrace.debug("UtFormat", "toCaption()", "nCounterJ="
                                                  + nCounterJ + ", straObjNum[nCounterJ]="
                                                  + straObjNum[nCounterJ]);
                                    if (straObjNum[nCounterJ].compareTo("零") == 0) {
                                            SSTrace.debug("UtFormat", "toCaption()", "nCounterJ="
                                                      + nCounterJ
                                                      + ", compare OK, straObjNum[nCounterJ+1]="
                                                      + straObjNum[nCounterJ + 1]);
                                            if (straObjNum[nCounterJ + 1] != null) {
                                                    if ( (straObjNum[nCounterJ + 1].compareTo("万") == 0)
                                                              || (straObjNum[nCounterJ + 1].compareTo("亿") == 0)
                                                              || (straObjNum[nCounterJ + 1].compareTo("元") == 0)
                                                              || (straObjNum[nCounterJ + 1].compareTo("零") == 0)) {
                                                            straObjNum[nCounterJ] = "_";
                                                    }
                                            }
                                    }
                                    nCounterJ++;
                            }
                            SSTrace.debug("UtFormat", "toCaption()", "nCounterJ=" + nCounterJ);
                            if (straObjNum[nCounterJ - 1].compareTo("零") == 0) {
                                    //消除最后的"零"字
                                    straObjNum[nCounterJ - 1] = "_";
                            }
                            nCounterJ = 0;
                            nCounterI = 0;
                            while (straObjNum[nCounterJ] != null) { //消除所有"_"符号
                                    if (straObjNum[nCounterJ].compareTo("_") != 0) {
                                            straTmp[nCounterI] = straObjNum[nCounterJ];
                                            nCounterI++;
                                    }
                                    nCounterJ++;
                            }
                            nObjLen = nCounterI;
                            for (nCounterI = 0; nCounterI < nObjLen; nCounterI++) { //消除"亿"后的"万"字
                                    strbTmp.append(straTmp[nCounterI]);
                                    if ( (straTmp[nCounterI].compareTo("亿") == 0)
                                        && (straTmp[nCounterI + 1].compareTo("万") == 0)) {
                                            straTmp[nCounterI + 1] = "";
                                    }
                            }
                            strNoCaption = strbTmp.append("整").toString();
                    }
                    return strNoCaption;
            }
      

  3.   

    function NumToChar(n:Real): wideString; //可以到万亿,并可随便扩大
    const
      cNum:WideString='零壹贰叁肆伍陆柒捌玖-万仟佰拾亿仟佰拾万仟佰拾元角分';
      cCha:array[0..1, 0..11]of string =
          (( '零仟','零佰','零拾','零零零','零零','零亿','零万','零元','亿万','零角','零分','零整'),
          ( '零','零','零','零','零','亿','万','元','亿','零','整','整'));
    var
      i :Integer;
      sNum :WideString;
    begin
      result :='';
      sNum := FormatFloat('0',n*100);
      for i := 1 to Length(sNum) do
        result := result + cNum[ord(sNum[i])-47] + cNum[26-Length(sNum)+i];
      for i:= 0 to 11 do //去掉多余的零
        result := StringReplace(result, cCha[0,i], cCha[1,i], [rfReplaceAll]);
    end;