如果是任意数字转英文大写,那位高手写全了,送100分! (100分)

  • 主题发起人 samboy111
  • 开始时间
S

samboy111

Unregistered / Unconfirmed
GUEST, unregistred user!
//不能转小数,好象有最大值限制,因是很久前写的,没仔细看了
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Edit1.Text:=NumberBig(Edit1.Text);
end;

function TForm1.NumberBig(Num: String): String;
Var
i:Integer;
StrTemp1,StrTemp2,Str:String;
NumTemp1:Integer; //每位数字
DoubZero,wanzero,ThreeZero:Boolean; //判断十,个位是否同时为零
OneB:Boolean; //判断第二位是否为1
unitStr:TStringList; //单位字符
begin
unitStr:=TStringList.Create;
unitStr.Add('HUNDRED');
unitStr.Add('THOUSAND');
unitStr.Add('MILLION');
StrTemp1:=Num;
For i:=1 To Length(StrTemp1) Do
Str:=StrTemp1+Str;
StrTemp1:= Str ;
Str:='';
For i:=1 to Length(StrTemp1) Do
Begin
NumTemp1:=StrToInt(StrTemp1);
If OneB=True Then
Begin
OneB:=False;
Continue;
End;
If ((i=1) or (i=4) OR (i=7)) and (StrTemp1[i+1]='1') Then
Begin
Case NumTemp1 Of
0:StrTemp2:=' TEN ';
1:StrTemp2:=' ELEVEN ';
2:StrTemp2:=' TWELVE ';
3:StrTemp2:=' THIRTEEN ';
4:StrTemp2:=' FOURTEEN ';
5:StrTemp2:=' FIFTEEN ';
6:StrTemp2:=' SIXTEEN ';
7:StrTemp2:=' SEVENTEEN ';
8:StrTemp2:=' EIGHTEEN ';
9:StrTemp2:=' NINETEEN ';
End;
OneB:=True;
End
Else If (i=2) or (i=5) OR (i=8) Then
Case NumTemp1 Of
0:StrTemp2:='';
1:StrTemp2:=' TEN ';
2:StrTemp2:=' TWENTY ';
3:StrTemp2:=' THIRTY ';
4:StrTemp2:=' FORTY ';
5:StrTemp2:=' FIFTY ';
6:StrTemp2:=' SIXTY ';
7:StrTemp2:=' SEVENTY ';
8:StrTemp2:=' EIGHTY ';
9:StrTemp2:=' NINETY ';
End
Else
Case NumTemp1 Of
0:StrTemp2:='';
1:StrTemp2:='ONE';
2:StrTemp2:='TWO';
3:StrTemp2:='THREE';
4:StrTemp2:='FOUR';
5:StrTemp2:='FIVE';
6:StrTemp2:='SIX';
7:StrTemp2:='SEVEN';
8:StrTemp2:='EIGHT';
9:StrTemp2:='NINE';
End ;
If StrTemp2='' Then //当前位是否为零
wanzero:=True;
If i=3 Then
Begin
If wanzero=False Then
Begin
If DoubZero=True Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[0]+' AND '
Else
StrTemp2:=StrTemp2+' '+unitStr.Strings[0];
End
Else
Begin
If DoubZero=True Then
StrTemp2:=StrTemp2+' AND ';
End;
End
Else If (i=4) and (wanzero=False) Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[1]+' '
Else If (i=5) and (wanzero=False) and (ThreeZero=False) Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[1]+' '
Else If (i=6) and (wanzero=False) Then
Begin
If ThreeZero=False Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[0]+' '+unitStr.Strings[1]+' '
Else
StrTemp2:=StrTemp2+' '+unitStr.Strings[0]+' ';
End
Else If i=7 Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[2]+' '
Else If (i=9) and (wanzero=False) Then
StrTemp2:=StrTemp2+' '+unitStr.Strings[0];
If StrTemp2<>'' Then //某位之前的数是否都为零
DoubZero:=True;
If (i>3) and (StrTemp2<>'') Then //4,5,6位是否同时为零
ThreeZero:=True;
wanzero:=False;
Str:=StrTemp2+Str;
END;
unitStr.Free;
Result:=Str;
end;

 
镐笑。。。
 
这和小写金额转换成大写金额有什么区别?
 
小意思了,我很早就在Excel中編寫了一個自定義函數,我想在delphi中也差不多一樣.
 
那位手头有,帖一下。
 
搞全了,还真不容易!期待哪位高手能够成功!
 
中文大写倒是有一个
英文应该差不多
 
你是数字一个一个单独转换呢?还是整体转换?如果是一个一个的数字单独转换,用case语句就行了,如果要判断整体转换的话,可能稍有些困难,我这有一个这样的例子,本已完工了,但在测试时,有一个数字溢出判断报错的bug,改完了可以给你!
 
12315615647.36
大写英文金额:
 
我觉得你提的问题有点笼统,任意数字转化为英文。
3748= three seven four enghit
还是 别的呢。
 
是你给我发的信息吗?我当时正下网,没看到,后来上来才看见,抱歉!
给你两个我收藏的函数吧,第一个是borton大侠写的,第二个忘了作者,都不错的。
你自己将大写中文改成英文吧,当然还有一些别的地方,自己改改吧。

[函数1]
下面函数尚有不足,当金额超过一千万亿元时,就出错了!但基本也没什么意义了
似乎百万亿也出错了!!!
function MoneyName(Value: Double): string;
const
SCnNumber = '零壹贰叁肆伍陆柒捌玖';
SCnPower = '拾佰仟';
var
V, V1: Double;
X: array[0..4] of Integer; //分别表示万亿位、亿位、万位、元位、分位
N, P, I, J: Integer; //内部使用
S: array[0..4] of string; //目标串
B: array[0..4] of Boolean; //是否零前缀
BK, BL: Boolean;
begin
V := Int(Value);
X[4] := Trunc((Value - V) * 100 + 0.5); //分位
X[0] := 0; //万亿位
X[1] := 0; //亿位
X[2] := 0; //万位
X[3] := 0; //元位
I := 3;
while (V > 0) and (I >= 0) do
begin
V1 := Int(V / 10000) * 10000;
X := Trunc(V - V1);
Dec(I);
V := V1 / 10000;
end;
BL := True; //检查是否全为零
for I := 0 to 4 do
if X <> 0 then
begin
BL := False;
Break;
end;
if BL then
Result := '零元整'
else
begin
//先计算整数部分每节的串
for I := 0 to 3 do
begin
S := '';
if X > 0 then
begin
B := False;
P := 1000;
BK := False; //前位为零
BL := False; //未记录过
for J := 0 to 3 do
begin
N := X div P; //当前位
X := X - N * P; //剩余位
P := P div 10; //幂
if N = 0 then //当前位为零
begin
if J = 0 then
B := True //如果是最高位
else
if BL then //如果未记录过
BK := True;
end
else
begin
if BK then
S := S + '零';
BL := True;
S := S + Copy(SCnNumber, N * 2 + 1, 2);
if J < 3 then
S := S + Copy(SCnPower, (3 - J) * 2 - 1, 2);
BK := False;
end;
end;
end;
end;
//小数部分
BL := False;
if X[4] mod 10 > 0 then
S[4] := Copy(SCnNumber, (X[4] mod 10) * 2 + 1, 2) + '分'
else
begin
BL := True;
S[4] := '';
end;
X[4] := X[4] div 10;
if X[4] > 0 then
begin
S[4] := Copy(SCnNumber, (X[4] mod 10) * 2 + 1, 2) + '角' + S[4];
B[4] := False;
end
else
B[4] := not BL;
//合并串
Result := '';
BL := False;
for I := 0 to 3 do
if Length(S) > 0 then
begin
if BL then
if B then
Result := Result + '零';
Result := Result + S;
case I of
0, 2: Result := Result + '万';
1: Result := Result + '亿';
3: Result := Result + '元';
end;
BL := True;
end
else
if BL then
case I of
1: Result := Result + '亿';
3: Result := Result + '元';
end;
if Length(S[4]) = 0 then
Result := Result + '整'
else
begin
if B[4] then
if BL then
Result := Result + '零';
Result := Result + S[4];
end;
end;
end;

=========================================================================
[函数2]
这个函数也不错!!
function SmallToBig(const ACurrency: Currency): string;
const
t_num: array[0..9] of string[2] =
('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
t_unit1: array[0..4] of string[2] =
('圆', '万', '亿', '万', '亿');
t_unit2: array[0..2] of string[2] =
('拾', '佰', '仟');
t_unit3: array[0..1] of string[2] =
('角', '分');
var
szCurrency: string;
dotPos: integer;
szInt: string; // 小写的整数部分
szFlt: string; // 小写的小数部分
sInt: string; // 大写的整数部分
sFlt: string; // 大写的小数部分
old_inx_num: integer;
inx_num: integer;
inx_unit: integer;
i: integer;
bOnlyFlt: Boolean;
begin
szCurrency := FormatCurr('0.00', ACurrency);
old_inx_num := 0;
dotPos := Pos('.', szCurrency);
if dotPos <> 0 then
begin
szInt := Copy(szCurrency, 0, dotPos - 1);
szFlt := Copy(szCurrency, dotPos + 1, 2);
end
else begin
szInt := szCurrency;
szFlt := '';
end;

if (szInt = '0') and ((szFlt = '00') or (szFlt = '')) then
begin
result := '零圆整';
exit;
end;

bOnlyFlt := szInt = '0'; // 如果整数部分为零

if (Length(szFlt) <> 2) and (dotPos <> 0) then
// 小数点后必须有两位或者根本没有
begin
result := '';
exit;
end;

if not bOnlyFlt then // 如果整数部分为零,则不对其进行处理
for i := 0 to Length(szInt) - 1 do
begin
inx_num := StrToInt(szInt[Length(szInt) - i]);
if i mod 4 = 0 then
begin
inx_unit := i div 4;
if (inx_num > 0) then
// 以下判断处理诸如"壹亿圆",“壹亿亿圆"
if (inx_unit > 0) and (inx_unit mod 2 = 0) and (sInt[1] + sInt[2] = '万') then
sInt := t_num[inx_num] + t_unit1[inx_unit] + copy(sInt, 3, 2 * (Length(sInt) - 1))
else
sInt := t_num[inx_num] + t_unit1[inx_unit] + sInt
else
// 以下判断处理诸如"壹拾亿圆“,"壹拾亿亿圆"
if (inx_unit > 0) and (inx_unit mod 2 = 0) and (sInt[1] + sInt[2] = '万') then
sInt := t_unit1[inx_unit] + copy(sInt, 3, 2 * (Length(sInt) - 1))
else
sInt := t_unit1[inx_unit] + sInt;
end
else begin
inx_unit := i mod 4 - 1;
if inx_num > 0 then
sInt := t_num[inx_num] + t_unit2[inx_unit] + sInt
else
if old_inx_num > 0 then
sInt := t_num[inx_num] + sInt;
end;
old_inx_num := inx_num;
end;

//处理小数部分
if (szFlt <> '') and (szFlt <> '00') then
begin
for i := 0 to 1 do
begin
inx_num := StrToInt(szFlt[i + 1]);
if (inx_num > 0) then
sFlt := sFlt + t_num[inx_num] + t_unit3
else
if i <> 1 then // 如果"角"为0 (分"是 0, 则忽略)
if not bOnlyFlt then // 如果整数为此时为零
sFlt := sFlt + t_num[inx_num];
end;
result := sInt + sFlt;
end
else begin
result := sInt + '整';
end
end;
 
小笨苯, 好
是我发的信息,我是要英文大写金额的函数,自己水平浅,只能烦劳大伙了。
 
拜托那位能写个完整的,本人水平实在很低,谢谢!
 
你留下E-MAIL吧,我发给你我自己写的EXCEL的函数!
 
wangydm,好人,谢谢你
samboy111@sohu.com
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
660
import
I
顶部