在水晶報表,公式工作室中新建報表自定義函數,函數內容如下:
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 := decimals1 mod 1000;
decimals1 := Int(decimals1 / 1000);
);
stringVar array groupText1 := ["","","",""];
//處理每段的金額轉英文,百位+十位+個位
for i:=1 to 4 step 1 do
(
numberVar hundreds := Int(digitGroups / 100);
numberVar tensUnits := digitGroups mod 100;
//百位
if hundreds <> 0 then
(
groupText1 := groupText1 + smallNumbers[hundreds+1] + " HUNDRED";
if tensUnits <> 0 then
groupText1 := groupText1 + " ";
);
//十位和個位
numberVar tens := Int(tensUnits / 10);
numberVar units := tensUnits mod 10;
if tens >= 2 then //十位大于等于20
(
groupText1 := groupText1 + tensNumbers[tens+1];
if units <> 0 then
groupText1 := groupText1 + " " + smallNumbers[units+1];
)
else if tensUnits <> 0 then //十位和個位,小于20的情況
groupText1 := groupText1 + smallNumbers[tensUnits +1]
);
//金額的個十百位賦值到combined
combined1 := groupText1[1];
//將金額排除個十百位以外,余下的3段英文數字,加上千位分隔符英文單詞,Thousand/Million/Billion
for i:=2 to 4 step 1 do
(
if digitGroups <> 0 then
(
stringVar prefix := groupText1 + " " + scaleNumers; //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";