高分求金额转换成大写的源码(300分)

  • 主题发起人 主题发起人 flai
  • 开始时间 开始时间
F

flai

Unregistered / Unconfirmed
GUEST, unregistred user!
如将 405.23 转换成 肆佰零伍圆贰角叁分

由于急用,因此没时间自己写

先行谢过!

flai@263.sina.com
 
抄一段给你!
:)
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] = ( '角', '分');

function SmallToBig ( szCurrency: string ): string;
var 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

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;

if ( szInt = '0' ) then bOnlyFlt := true; // 如果整数部分为零


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;
 
等下,我會發給你的!
 
转贴一个,忘了作者,希望不会被骂。。。
function TForm1.NumToChnStr(Value: Real; ClearZero: Boolean): String;
const
ChnUnit: array[0..13] of string = ('分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟');
ChnNum : array[0..9] of string = ('零', '壹','贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
var
I: Integer;
StrValue, StrNum: String;
ValueLen: Integer;
begin
if Value <= 0 then
begin
Result := '输入参数应大于零。';
Exit;
end;
StrValue := IntToStr(Round(Value * 100));
ValueLen := Length(StrValue);
Result := '';
for I := 1 to ValueLen do
begin
StrNum := StrValue;
Result := Result + ChnNum[StrToInt(StrNum)] + ChnUnit[ValueLen - I];
end;
if ClearZero then
begin
Result := StringReplace(Result, '零分', '', [rfReplaceAll]);
Result := StringReplace(Result, '零角', '', [rfReplaceAll]);
Result := StringReplace(Result, '零元', '元', [rfReplaceAll]);
Result := StringReplace(Result, '零拾', '', [rfReplaceAll]);
Result := StringReplace(Result, '零佰', '', [rfReplaceAll]);
Result := StringReplace(Result, '零仟', '', [rfReplaceAll]);
Result := StringReplace(Result, '零万', '万', [rfReplaceAll]);
end;
end;
 
//飘摇客整理
//如下所示,新建一个Application,然后增加一个函数xTod,增加以下代码,然后在窗体上放一个按钮。

function TForm1.xTOd(i:Real):string;
const
d='零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿';
var
m,k:string;
j:integer;
begin
k:='';
m:=floattostr(int(i*100));
for j:=length(m) downto 1 do
k:=k+d[(strtoint(m[Length(m)-j+1])+1)*2-1]+
d[(strtoint(m[Length(m)-j+1])+1)*2]+d[(10+j)*2-1]+d[(10+j)*2];
xTOd:=k;
end;

调用:
procedure TForm1.Button1Click(Sender: TObject);
var
Sum:real;
begin
sum:=12.34;
showmessage('人民币大写:'+xTOd(Sum));
end;

 
function TForm1.xTOd(i:Real):string;
const
d='零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿';
var
m,k:string;
j:integer;
begin
k:='';
m:=floattostr(int(i*100));
for j:=length(m) downto 1 do
k:=k+d[(strtoint(m[Length(m)-j+1])+1)*2-1]+
d[(strtoint(m[Length(m)-j+1])+1)*2]+d[(10+j)*2-1]+d[(10+j)*2];
xTOd:=k;
end;

///调用:
procedure TForm1.Button1Click(Sender: TObject);
var
Sum:real;
begin
sum:=12.34;
showmessage('人民币大写:'+xTOd(Sum));
end;
 
转帖xWolf,比较全面,很好用
unit Chnum;

interface

uses SysUtils, Dialogs;

var
chn_number: array [0..9] of string;

function chn_intvalue(ivalue: integer): string;
function chn_money(money: double): string;

implementation

var
chn_sectionunit: array [0..2] of string;

//=================================================================
function chn_intvalue(ivalue: integer): string;
var
intstr, signstr, tstr: string;
q_digit, b_digit, s_digit, g_digit: string;
len, sectionlevel: integer;
zeroresulthead, zerohead: boolean;
begin
if (ivalue=0) then
begin
result:='零';
exit;
end
else if (ivalue<0) then
begin
signstr:='负';
ivalue:=-ivalue;
end
else signstr:='';
intstr:=inttostr(ivalue);
len:=Length(intstr);

sectionlevel:=-1; //以4位数字为一节: eg. "万"节; "亿"节;
zeroresulthead:=false;
//跨节用途.(指明上一循环后产生的中间结果(低位"节")是否为"零"开头)
while (len>0) do
begin
inc(sectionlevel);
if (sectionlevel>=3) then // 限定 1 兆以内.
begin
showmessage('chn_intvalue error: value('+intstr
+') result in overflow.');
abort;
end;
//取得本节各位的数值, 并更新数据源intstr.
g_digit:=Copy(intstr, len, 1); // 节内"个"位.
if (len>1) then s_digit:=Copy(intstr, len-1, 1)
else s_digit:=''; // 节内"十"位.
if (len>2) then b_digit:=Copy(intstr, len-2, 1)
else b_digit:=''; // 节内"百"位.
if (len>3) then q_digit:=Copy(intstr, len-3, 1)
else q_digit:=''; // 节内"千"位.
if (len>4) then Delete(intstr, len-3, 4)
else intstr:='';
len:=Length(intstr);
//对本节进行中文翻译.
zerohead:=false;
if (g_digit<>'0') then tstr:=chn_number[strtoint(g_digit)]
else tstr:=''; //完成"个"位中文翻译.
if (s_digit='0') then s_digit:=''; //完成"拾"位中文翻译.
if (s_digit<>'') then
begin
tstr:=chn_number[strtoint(s_digit)]+'拾'+tstr;
zerohead:=false;
end
else if (tstr<>'') then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (b_digit='0') then b_digit:=''; //完成"佰"位中文翻译.
if (b_digit<>'') then
begin
tstr:=chn_number[strtoint(b_digit)]+'佰'+tstr;
zerohead:=false;
end
else if (tstr<>'') and not(zerohead) then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (q_digit='0') then q_digit:=''; //完成"千"位中文翻译.
if (q_digit<>'') then
begin
tstr:=chn_number[strtoint(q_digit)]+'仟'+tstr;
zerohead:=false;
end
else if (tstr<>'') and not(zerohead) then
begin
tstr:='零'+tstr;
zerohead:=true;
end;
if (tstr<>'') then
begin
result:=tstr+chn_sectionunit[sectionlevel]+result;
zeroresulthead:=zerohead;
end
else if not(zeroresulthead) then
begin
result:='零'+result;
zeroresulthead:=true;
end;
end; // of while
if (zeroresulthead) then Delete(result, 1, 2); // 除首"零"
result:=signstr+result;
end;
//-----------------------------------------------------------------
function chn_money(money: double): string;
var
intpart, chiao, cent: integer;
fracpart: double;
signstr, tstr: string;
begin
signstr:='';
if (money<0) then
begin
money:=-money;
signstr:='负';
end;
money:=money+0.005; //包含四舍五入
intpart:=Trunc(money); //Trunc truncates a real number to an integer.
fracpart:=money-intpart;
fracpart:=fracpart*10;
chiao:=Trunc(fracpart);
fracpart:=fracpart-chiao;
fracpart:=fracpart*10;
cent:=Trunc(fracpart);
tstr:=chn_intvalue(intpart);
if (chiao=0) and (cent=0) then
result:=tstr+'元整';
if (chiao <> 0) and (cent = 0) then
result:=tstr+'元'+chn_number[chiao]+'角整';
if (chiao=0) and (cent <> 0) then
result:=tstr+'元'+'零'+chn_number[cent]+'分';
if (chiao <> 0) and (cent <> 0) then
result:=tstr+'元'+chn_number[chiao]+'角'+chn_number[cent]+'分';
result:=signstr+result;
end;
//=================================================================
initialization
chn_number[0]:='零';
chn_number[1]:='壹';
chn_number[2]:='贰';
chn_number[3]:='叁';
chn_number[4]:='肆';
chn_number[5]:='伍';
chn_number[6]:='陆';
chn_number[7]:='柒';
chn_number[8]:='捌';
chn_number[9]:='玖';

chn_sectionunit[0]:='';
chn_sectionunit[1]:='万';
chn_sectionunit[2]:='亿';

end.
 
已發送,請接收!
 
这是我的程序中用的函数

function TCash_Login_Form.Convert(const hjnum: real): string;
var
Vstr, zzz, cc, cc1, Presult: string;
xxbb: array[1..12] of string;
uppna: array[0..9] of string;
iCount, iZero, vPoint, vdtlno: integer;
begin
//*设置大写中文数字和相应单位数组*//
xxbb[1] := '亿';
xxbb[2] := '仟';
xxbb[3] := '佰';
xxbb[4] := '拾';
xxbb[5] := '万';
xxbb[6] := '仟';
xxbb[7] := '佰';
xxbb[8] := '拾';
xxbb[9] := '元';
xxbb[10] := '.';
xxbb[11] := '角';
xxbb[12] := '分';
uppna[0] := '零';
uppna[1] := '壹';
uppna[2] := '贰';
uppna[3] := '叁';
uppna[4] := '肆';
uppna[5] := '伍';
uppna[6] := '陆';
uppna[7] := '柒';
uppna[8] := '捌';
uppna[9] := '玖';
Str(hjnum: 12: 2, Vstr);
cc := '';
cc1 := '';
zzz := '';
result := '';
presult := '';
iZero := 0;
vPoint := 0;
for iCount := 1 to 10 do
begin
cc := Vstr[iCount];
if cc <> ' ' then
begin
zzz := xxbb[iCount];
if cc = '0' then
begin
if iZero < 1 then //*对“零”进行判断*//
cc := '零'
else
cc := '';
if iCount = 5 then //*对万位“零”的处理*//
if copy(result, length(result) - 1, 2) = '零' then
result := copy(result, 1, length(result) - 2) + xxbb[iCount]
+ '零'
else
result := result + xxbb[iCount];
cc1 := cc;
zzz := '';
iZero := iZero + 1;
end
else
begin
if cc = '.' then
begin
cc := '';
if (cc1 = '') or (cc1 = '零') then
begin
Presult := copy(result, 1, Length(result) - 2);
result := Presult;
iZero := 15;
end;
if iZero >= 1 then
zzz := xxbb[9]
else
zzz := '';
vPoint := 1;
end
else
begin
iZero := 0;
cc := uppna[StrToInt(cc)];
end
end;
result := result + (cc + zzz)
end;
end;
if Vstr[11] = '0' then //*对小数点后两位进行处理*//
begin
if Vstr[12] <> '0' then
begin
cc := '零';
result := result + cc;
cc := uppna[StrToInt(Vstr[12])];
result := result + (uppna[0] + cc + xxbb[12]);
end
end
else
begin
if iZero = 15 then
begin
cc := '零';
result := result + cc;
end;
cc := uppna[StrToInt(Vstr[11])];
result := result + (cc + xxbb[11]);
if Vstr[12] <> '0' then
begin
cc := uppna[StrToInt(Vstr[12])];
result := result + (cc + xxbb[12]);
end;
end;
result := result + '整';
end;
 
太感动了

最后我还是采用了 薛獅 的code,挺好的,嘻嘻
 

Similar threads

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