下面这个函数是最好的,是barton大侠写的,其他很多人写的都有Bug,不过,这个函数也有一点不足, 当金额超过一千万亿元时,就出错了!但我们这一辈子恐怕也用不到这么大的金额,所以基本也没什么意义了。 如果barton大侠进来了,分数就给他。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[I] := Trunc(V - V1); Dec(I); V := V1 / 10000; end; BL := True; //检查是否全为零 for I := 0 to 4 do if X[I] <> 0 then begin BL := False; Break; end; if BL then Result := '零元整' else begin //先计算整数部分每节的串 for I := 0 to 3 do begin S[I] := ''; if X[I] > 0 then begin B[I] := False; P := 1000; BK := False; //前位为零 BL := False; //未记录过 for J := 0 to 3 do begin N := X[I] div P; //当前位 X[I] := X[I] - N * P; //剩余位 P := P div 10; //幂 if N = 0 then //当前位为零 begin if J = 0 then B[I] := True //如果是最高位 else if BL then //如果未记录过 BK := True; end else begin if BK then S[I] := S[I] + '零'; BL := True; S[I] := S[I] + Copy(SCnNumber, N * 2 + 1, 2); if J < 3 then S[I] := S[I] + 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[I]) > 0 then begin if BL then if B[I] then Result := Result + '零'; Result := Result + S[I]; 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;
我自己写了一个,代码更少一些,在我的AC Report里已经应用:var Cdigs:array[0..9] of string=('零','壹','贰','叁','肆','伍', '陆','柒','捌','玖'); Cdsets:array[1..18] of string=('万','仟','佰','拾','亿', '仟','佰','拾','万','仟','佰','拾','圆','','角','分','厘','毫');Function GetCmoney(const Outv :real):string; var DStr,outStr,tmpstr: string; dsize,i:integer; CanZ: boolean; begin CanZ := False; Dstr := formatfloat('0.0000',Outv); dsize := length(Dstr); outStr :=''; for i:=dsize Downto 1 do begin if Dstr[i]='0' then begin if Canz then begin CanZ := False; tmpstr := '零' end else tmpstr := ''; if (CdSets[18-(Dsize-i)]='圆') or (CdSets[18-(Dsize-i)]='万') or (CdSets[18-(Dsize-i)]='亿') then tmpstr := CdSets[18-(Dsize-i)]+tmpstr; end else if Dstr[i]='.' then begin // if Copy(dstr,dsize-3,4)='0000' then // tmpstr := '整' tmpstr := ''; end else if Dstr[i]='-' then TmpStr := '负' else begin tmpStr := Cdigs[StrToInt(Dstr[i])] + CdSets[18-(Dsize-i)]; if Not CanZ then CanZ := true; end; OutStr := TmpStr+OutStr; end; if Copy(dstr,dsize-2,3)='000' then OutStr :=OutStr+ '整'; Result := OutStr; end;
当金额超过一千万亿元时,就出错了!但我们这一辈子恐怕也用不到这么大的金额,所以基本也没什么意义了。
如果barton大侠进来了,分数就给他。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[I] := Trunc(V - V1);
Dec(I);
V := V1 / 10000;
end;
BL := True; //检查是否全为零
for I := 0 to 4 do
if X[I] <> 0 then
begin
BL := False;
Break;
end;
if BL then
Result := '零元整'
else
begin
//先计算整数部分每节的串
for I := 0 to 3 do
begin
S[I] := '';
if X[I] > 0 then
begin
B[I] := False;
P := 1000;
BK := False; //前位为零
BL := False; //未记录过
for J := 0 to 3 do
begin
N := X[I] div P; //当前位
X[I] := X[I] - N * P; //剩余位
P := P div 10; //幂
if N = 0 then //当前位为零
begin
if J = 0 then
B[I] := True //如果是最高位
else
if BL then //如果未记录过
BK := True;
end
else
begin
if BK then
S[I] := S[I] + '零';
BL := True;
S[I] := S[I] + Copy(SCnNumber, N * 2 + 1, 2);
if J < 3 then
S[I] := S[I] + 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[I]) > 0 then
begin
if BL then
if B[I] then
Result := Result + '零';
Result := Result + S[I];
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;
這個也不錯, 可參考
FASTREPORT中有函数可以的
好象是STRTORMB
Cdigs:array[0..9] of string=('零','壹','贰','叁','肆','伍',
'陆','柒','捌','玖');
Cdsets:array[1..18] of string=('万','仟','佰','拾','亿',
'仟','佰','拾','万','仟','佰','拾','圆','','角','分','厘','毫');Function GetCmoney(const Outv :real):string;
var DStr,outStr,tmpstr: string;
dsize,i:integer;
CanZ: boolean;
begin
CanZ := False;
Dstr := formatfloat('0.0000',Outv);
dsize := length(Dstr);
outStr :='';
for i:=dsize Downto 1 do
begin
if Dstr[i]='0' then
begin
if Canz then
begin
CanZ := False;
tmpstr := '零'
end
else tmpstr := ''; if (CdSets[18-(Dsize-i)]='圆') or
(CdSets[18-(Dsize-i)]='万') or
(CdSets[18-(Dsize-i)]='亿')
then tmpstr := CdSets[18-(Dsize-i)]+tmpstr;
end
else
if Dstr[i]='.' then
begin
// if Copy(dstr,dsize-3,4)='0000' then
// tmpstr := '整'
tmpstr := '';
end
else if Dstr[i]='-' then
TmpStr := '负'
else
begin
tmpStr := Cdigs[StrToInt(Dstr[i])] + CdSets[18-(Dsize-i)];
if Not CanZ then
CanZ := true;
end;
OutStr := TmpStr+OutStr;
end;
if Copy(dstr,dsize-2,3)='000' then
OutStr :=OutStr+ '整';
Result := OutStr;
end;