代码:(复制下面的代码直接在报表新建一个Formula就可以用了。Crystal Report 版本:8.5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
numbervar Amount:=Sum ({@Amount}); //{@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"
;
|