水晶报表下将数字转换成金额大写的公式

本文介绍了一种将阿拉伯数字转换为中文大写金额的方法,并提供了两种不同的实现方式:一种忽略中间的零,另一种则显示所有位置的零。通过具体代码展示了如何在水晶报表中实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本一:这种金额大写忽略不同进度间的零的显示,显示效果如:二十万九百一元四角整(200901.40)

ContractedBlock.gifExpandedBlockStart.gifCode
StringVar x; 
StringVar xx :
= ""
NumberVar z; 
NumberVar i; 
ExpandedBlockStart.gifContractedBlock.gifx :
= ToText(Sum ({@@AMOUNT})); 
x :
= Replace(x,",",""); 
x :
= Replace(x,".",""); 
z :
= length(x)-2

for i := z to -1 step -1 do 

    select i 
    
case 10 : if(Mid(x,z-i+1,1<>"0") then xx := Mid(x,z-i+1,1+ "" 
    
case 9 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "亿" else xx := xx + "亿" 
    
case 8 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 7 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 6 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 5 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" else xx := xx + "" 
    
case 4 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 3 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 2 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1+ "" 
    
case 1 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1)+"" 
    
case 0 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1)+"" 
    
case -1 : if(Mid(x,z-i+1,1<>"0") then xx := xx + Mid(x,z-i+1,1else xx :=xx+"" 
); 
xx :
= Replace(xx,"1",""); 
xx :
= Replace(xx,"2",""); 
xx :
= Replace(xx,"3",""); 
xx :
= Replace(xx,"4",""); 
xx :
= Replace(xx,"5",""); 
xx :
= Replace(xx,"6",""); 
xx :
= Replace(xx,"7",""); 
xx :
= Replace(xx,"8",""); 
xx :
= Replace(xx,"9",""); 
if instr(xx,''<=0 then 
  
if instr(xx,'')>0 then 
    xx:
=left(xx,instr(xx,'')-2)+''+right(xx,len(xx)-instr(xx,'')+2); 
  

if right(xx,1)='' then 
    
if instr(xx,'')>0 then 
      xx :
=left(xx,len(xx)-1)+'' 
    
else 
      xx :
=left(xx,len(xx)-1)+'元整'  
else  
  xx :
=xx + "";

 

由于客户对于报表金额的显示要求,需要将不同位将的零也显示出来,故修改了一下版本,值得注意的是Replace这个方法在水晶报表中除了替代功能外还有截取功能,例如“abcdefg字符” Replace(“abcdefg”,“c”,2,1)的返回值为defg,所以用left来找回之前的数字,在这里我很想鄙视水晶(公式函数说明一个也没有)

版本二:这种金额大写不同进度间的零的显示一次,显示效果如:二十万零九百零一元四角整(200901.40)

ContractedBlock.gifExpandedBlockStart.gifCode
StringVar x; 
StringVar xx:
="";
NumberVar z; 
NumberVar i; 
x :
= ToText(Sum ({BuyCompact.SC_SumPrice})); 
x :
= Replace(x,",",""); 
x :
= Replace(x,".",""); 
z :
= length(x);

for i := z to 1 step -1 do 
(
    StringVar temp:
=Mid(x,z-i+1,1);
    select i 
    
case 12 : xx := temp + ""
    
case 11 : xx := xx + temp + "亿"
    
case 10 : xx := xx + temp + ""
    
case 9 : xx := xx + temp + "" 
    
case 8 : xx := xx + temp + "" 
    
case 7 : xx := xx + temp + ""
    
case 6 : xx := xx + temp + "" 
    
case 5 : xx := xx + temp + "" 
    
case 4 : xx := xx + temp + "" 
    
case 3 : xx := xx + temp + "" 
    
case 2 : xx := xx + temp + "" 
    
case 1 : xx := xx + temp + ""
    
); 
stringvar test
="";
z :
= length(xx);
i:
=1;
while i<do 
(
    StringVar temp:
=Mid(xx,i,1);
    
if temp="0" then
    (
        
if mid(xx,i-2,1)<>"0" and (Mid(xx,i+1,1)="亿" or Mid(xx,i+1,1)="" or Mid(xx,i+1,1)="") then
        (
        xx :
=Left (xx,i-1 )+Replace(xx,Mid(xx,i,1) ,"" ,i , 1);
        )
        
else if mid(xx,i-2,1)<>"0" and i+2<length(xx) and mid(xx,i+2,1)<>"0" then
        (
        xx :
=Left (xx,i )+Replace(xx,Mid(xx,i+1,1) ,"" ,i+1 , 1);
        )
        
else if Mid(xx,i+1,1)="" and Mid(xx,i+2,1)<>"0" then
        (
        xx :
=Left (xx,i )+Replace(xx,Mid(xx,i+1,1) ,"" ,i+1 , 1);
        )
        
else
        (
        i :
= i-1;
        xx :
=Left (xx,i )+Replace(xx,Mid(xx,i+1,2) ,"" ,i+1 , 1);
        )
    );
    i:
=i+1;
    z:
=length(xx);
);

xx :
= Replace(xx,"0","");
xx :
= Replace(xx,"1",""); 
xx :
= Replace(xx,"2",""); 
xx :
= Replace(xx,"3",""); 
xx :
= Replace(xx,"4",""); 
xx :
= Replace(xx,"5",""); 
xx :
= Replace(xx,"6",""); 
xx :
= Replace(xx,"7",""); 
xx :
= Replace(xx,"8",""); 
xx :
= Replace(xx,"9",""); 
xx :
= xx + ""+test;

 

界面代码:

ContractedBlock.gifExpandedBlockStart.gif金额大写显示
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
        
/// 大写显示金额
        
/// </summary>
        
/// <param name="value"></param>

        public void UpperAccount(decimal value)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (value == 0)
                
return;
            
string strValue = value.ToString();
            
if (!strValue.Contains("."))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                strValue 
+= ".00";
            }

            
string strResult = string.Empty;
            
int z, i;
            strValue 
= strValue.Replace(".""");
            z 
= strValue.Length;
            
for (i = z; i >= 1; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string temp = strValue.Substring(z - i, 1);
                
switch (i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
case 12: strResult += temp + ""break;
                    
case 11: strResult += temp + "亿"break;
                    
case 10: strResult += temp + ""break;
                    
case 9: strResult += temp + ""break;
                    
case 8: strResult += temp + ""break;
                    
case 7: strResult += temp + ""break;
                    
case 6: strResult += temp + ""break;
                    
case 5: strResult += temp + ""break;
                    
case 4: strResult += temp + ""break;
                    
case 3: strResult += temp + ""break;
                    
case 2: strResult += temp + ""break;
                    
case 1: strResult += temp + ""break;
                }

            }

            z 
= strResult.Length;
            
while (i < z)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string temp = strResult.Substring(i, 1);
                
if (temp == "0" && i == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    i
++;
                    
continue;
                }

                
else if (temp == "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (strResult.Substring(i - 21!= "0" && (strResult.Substring(i + 11== "亿" || strResult.Substring(i + 11== "" || strResult.Substring(i + 11== ""))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        strResult 
= strResult.Remove(i, 1);
                    }

                    
else if (strResult.Substring(i - 21!= "0" && i + 2 < strResult.Length && strResult.Substring(i + 21!= "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        strResult 
= strResult.Remove(i + 11);
                    }

                    
else if (strResult.Substring(i + 11== "" && strResult.Substring(i + 21!= "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        strResult 
= strResult.Remove(i + 11);
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        i
--;
                        strResult 
= strResult.Remove(i + 12);
                    }

                }

                i
++;
                z 
= strResult.Length;
            }

            strResult 
= strResult.Replace("0""");
            strResult 
= strResult.Replace("1""");
            strResult 
= strResult.Replace("2""");
            strResult 
= strResult.Replace("3""");
            strResult 
= strResult.Replace("4""");
            strResult 
= strResult.Replace("5""");
            strResult 
= strResult.Replace("6""");
            strResult 
= strResult.Replace("7""");
            strResult 
= strResult.Replace("8""");
            strResult 
= strResult.Replace("9""");
            lblAccount.Text 
= strResult + "";
        }

 

转载于:https://www.cnblogs.com/FlyCloud/archive/2009/03/01/1400862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值