crystal report中英文数字转大写

在最近的项目中,需要将水晶报表里的金额总计,转换为英文大写。实现的效果如下图所示:

 

1

代码:(复制下面的代码直接在报表新建一个Formula就可以用了。Crystal Report 版本:8.5)

numbervar Amount:=Sum ({@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 := "AND CENTS " + tensNumbers[tens+1]; 
            if units <> 0 then 
                combined2 := combined2 + " " + smallNumbers[units+1]; 
        ) 
        else if decimals2 > 1 then //19到2之间 
                combined2 := "AND CENTS " + smallNumbers[decimals2 +1] 
        else 
                combined2 := "AND 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"; 

 
posted @ 2010-01-28 10:50  szny  阅读(1305)  评论(1编辑  收藏  举报