[水晶报表]金额转英文大写

源文转载:http://bbs.csdn.net/topics/300021579

Function (numbervar Amount) 
//金额转英文大写,Crystal语法
    local stringVar array smallNumbers := ["ZERO",  "ONE",  "TWO",  "THREE",  "FOUR",  "FIVE",  "SIX",  "SEVEN",  "EIGHT", 
                                            "NINE",  "TEN",  "ELEVEN",  "TWELVE",  "THIRTEEN",  "FOURTEEN",  "FIFTEEN", 
                                            "SIXTEEN",  "SEVENTEEN",  "EIGHTEEN",  "NINETEEN"]; 
    local stringVar array tensNumbers := ["",  "",  "TWENTY",  "THIRTY",  "FORTY",  "FIFTY",  "SIXTY",  "SEVENTY",  "EIGHTY", "NINETY"]; 
    local stringVar array scaleNumers := ["", "THOUSAND", "MILLION", "BILLION" ]; 
    local stringVar End := "ONLY";


    //小数点前
    local numberVar decimals1;
     //小数点后
    local numberVar decimals2;

    //纯小数
    If Amount <1 then
        decimals1 := 0
    else
        decimals1 := Cdbl(Left(Replace(cstr(Amount*100,0),",",""),length(Replace(cstr(Amount*100,0),",",""))-2));

    local numberVar decimals2 := Cdbl(Right(Replace(cstr(Amount*100,0),",",""),2));


    //初始化显示英文为ZERO
    stringVar combined1 := smallNumbers[1];
    stringVar combined2 := smallNumbers[1];

    if decimals1 <> 0 then 
    ( 
        numberVar i := 1; 
        numberVar array digitGroups := [0,0,0,0]; 

        //将金额拆分成4段,每段放3位数,即:XXX,XXX,XXX,XXX。最大仅支持到Billion,
        for i := 1  to 4 step 1  do 
        ( 
            digitGroups[i] := decimals1 mod 1000; 
            decimals1 := Int(decimals1 / 1000); 
        ); 

        stringVar array groupText1 := ["","","",""]; 

        //处理每段的金额转英文,百位+十位+个位
        for i:=1 to 4 step 1 do 
        ( 
            numberVar hundreds := Int(digitGroups[i] / 100); 
            numberVar tensUnits := digitGroups[i] mod 100; 

            //百位
            if hundreds <> 0 then  
            ( 
                groupText1[i] := groupText1[i] + smallNumbers[hundreds+1] + " HUNDRED"; 
                if tensUnits <> 0 then 
                    groupText1[i] := groupText1[i] + " AND "; 
            ); 

            //十位和个位
            numberVar tens := Int(tensUnits / 10); 
            numberVar units := tensUnits mod 10; 

            if tens >= 2 then //十位大于等于20
            ( 
                groupText1[i] := groupText1[i] + tensNumbers[tens+1]; 
                if units <> 0 then 
                    groupText1[i] := groupText1[i] + " " + smallNumbers[units+1]; 
            ) 
            else if tensUnits <> 0 then //十位和个位,小于20的情况
                groupText1[i] := groupText1[i] + smallNumbers[tensUnits +1] 
        ); 

        //金额的个十百位赋值到combined
        combined1 := groupText1[1]; 

         //将金额排除个十百位以外,余下的3段英文数字,加上千位分隔符英文单词,Thousand/Million/Billion
        for i:=2 to 4 step 1 do 
        ( 
            if digitGroups[i] <> 0 then 
            ( 
                stringVar prefix := groupText1[i] + " " + scaleNumers[i];  //A:组合Thousand 和Billion
                if Length(combined1) <> 0 then //B:如果金额的百位+十位+个位非0,则在后面加上空格
                    prefix := prefix+ " "; 
                combined1 := prefix + combined1; //再连接 A+B
            ); 
        ); 
    ); 

    if decimals2 <> 0 then 
    (
        //十位和个位
        numberVar tens := Int(decimals2 / 10); 
        numberVar units := decimals2 mod 10; 

        if decimals2 >= 20 then //20~99
        ( 
            combined2 := "CENTS " + tensNumbers[tens+1]; 
            if units <> 0 then 
                combined2 := combined2 + " " + smallNumbers[units+1]; 
         )
        else if decimals2 > 1 then //192之间
                combined2 := "CENTS " + smallNumbers[decimals2 +1]
        else
                combined2 := "CENT " + smallNumbers[decimals2 +1]

    );

    if combined1 <> "ZERO" Then
        if combined2 <> "ZERO" Then
            combined1 +" "+ combined2 + " " + End
        else
            combined1+ " " + End
    else if combined2 <> "ZERO" Then
            combined2 + " " + End
         else
            "ZERO";
阅读更多

扫码向博主提问

厦门德仔

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • ERP
  • .NET
  • SQL
  • Ext.Net
  • DevExpress
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页