水晶报表下将数字转换成金额大写的公式
版本一:这种金额大写忽略不同进度间的零的显示,显示效果如:二十万九百一元四角整(200901.40)
Code
StringVar x;
StringVar xx := "";
NumberVar z;
NumberVar i;
x := 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,1) else 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 + "分";
StringVar x;
StringVar xx := "";
NumberVar z;
NumberVar i;
x := 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,1) else 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)
Code
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<z 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;
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<z 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;
界面代码:
金额大写显示
/**//// <summary>
/// 大写显示金额
/// </summary>
/// <param name="value">值</param>
public void UpperAccount(decimal value)
{
if (value == 0)
return;
string strValue = value.ToString();
if (!strValue.Contains("."))
{
strValue += ".00";
}
string strResult = string.Empty;
int z, i;
strValue = strValue.Replace(".", "");
z = strValue.Length;
for (i = z; i >= 1; i--)
{
string temp = strValue.Substring(z - i, 1);
switch (i)
{
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)
{
string temp = strResult.Substring(i, 1);
if (temp == "0" && i == 0)
{
i++;
continue;
}
else if (temp == "0")
{
if (strResult.Substring(i - 2, 1) != "0" && (strResult.Substring(i + 1, 1) == "亿" || strResult.Substring(i + 1, 1) == "万" || strResult.Substring(i + 1, 1) == "元"))
{
strResult = strResult.Remove(i, 1);
}
else if (strResult.Substring(i - 2, 1) != "0" && i + 2 < strResult.Length && strResult.Substring(i + 2, 1) != "0")
{
strResult = strResult.Remove(i + 1, 1);
}
else if (strResult.Substring(i + 1, 1) == "角" && strResult.Substring(i + 2, 1) != "0")
{
strResult = strResult.Remove(i + 1, 1);
}
else
{
i--;
strResult = strResult.Remove(i + 1, 2);
}
}
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 + "整";
}
/**//// <summary>
/// 大写显示金额
/// </summary>
/// <param name="value">值</param>
public void UpperAccount(decimal value)
{
if (value == 0)
return;
string strValue = value.ToString();
if (!strValue.Contains("."))
{
strValue += ".00";
}
string strResult = string.Empty;
int z, i;
strValue = strValue.Replace(".", "");
z = strValue.Length;
for (i = z; i >= 1; i--)
{
string temp = strValue.Substring(z - i, 1);
switch (i)
{
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)
{
string temp = strResult.Substring(i, 1);
if (temp == "0" && i == 0)
{
i++;
continue;
}
else if (temp == "0")
{
if (strResult.Substring(i - 2, 1) != "0" && (strResult.Substring(i + 1, 1) == "亿" || strResult.Substring(i + 1, 1) == "万" || strResult.Substring(i + 1, 1) == "元"))
{
strResult = strResult.Remove(i, 1);
}
else if (strResult.Substring(i - 2, 1) != "0" && i + 2 < strResult.Length && strResult.Substring(i + 2, 1) != "0")
{
strResult = strResult.Remove(i + 1, 1);
}
else if (strResult.Substring(i + 1, 1) == "角" && strResult.Substring(i + 2, 1) != "0")
{
strResult = strResult.Remove(i + 1, 1);
}
else
{
i--;
strResult = strResult.Remove(i + 1, 2);
}
}
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 + "整";
}