以下是一个Delphi源码,希望对你有帮助,虽然只能转换整形 function ChinaNum(X: Int64):string; const positions:Array[1..4] of string=( '仟','佰','拾','' );//四位一组的后缀 digits :Array[0..9] of string=( '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');//大写定义 var S,tmp: string; Str: Array[1..4] of String; Num: Array[1..4] of Integer; ChinaNum: Array[1..4] of String; I,J,K: Integer; begin if X>9999999999999999 then begin Result:= '超出范围'; Exit; end; S:=format('%16u',[x]);//将数字转化为16位长度的字符串(不足添空格) result:='';//返回值 ChinaNum[1]:='京';//单位 ChinaNum[2]:='亿'; ChinaNum[3]:='万'; ChinaNum[4]:=''; For I:= 1 to 4 do begin Str[I]:= Copy(S, (I shl 2)-3,4);//字符串拷贝从(i shl 2)-3的位置开始的4个.i shl 2 表示左移2位 if Str[I]='0000' then begin Str[I]:=' '; Continue; end else begin J:=Pos('000',Str[I]); case J of 0: begin J:=Pos('00', Str[I]); Case J of 0:; else Str[I,J]:=' '; end; end; else begin Str[I,J]:=' '; Str[I,J+1]:=' '; end; end; if Str[I,4]='0' then Str[I,4]:=' '; end; end; For I:= 1 to 4 do begin For J:= 4 Downto 1 do begin case Str[I,J] of '0': ChinaNum[I]:='零'+ChinaNum[I]; '1'..'9': begin ChinaNum[I]:=Digits[Ord(Str[I,J])-48]+Positions[J]+ChinaNum[I];//Ord函数 取字符ASCII码 end; else; end; end; if (I=4)Or(Length(ChinaNum[I])>2) then begin Result:= Result+ ChinaNum[I]; end; end; if Result='' then Result:='零'; end;
function FourNumToChnNum(Str:string;ChnNum:string;var Pre:boolean):string; const ChnNum2='零壹贰叁肆伍陆柒捌玖'; var i,j,Len:integer; begin Result := ''; Len := Length(str) ; for i:=1 to Len do begin j := Ord(str[i])-48; if j=0 then Pre := True else begin if Pre then Result := Result + '零'; Result := Result + Copy(ChnNum2,j*2+1,2) + Trim(Copy(ChnNum,(Len - i) * 2+1,2)); Pre := False; end; end; end;function StringToChnNum(str:string):string; const ChnNum1='圆万亿兆'; var i,Len,Len1,Level,Start:integer; s1,s:string; Pre: Boolean; begin Result := ''; Len := Pos('.',str)-1; Level := (Len + 3) div 4 ; Len1 := Len mod 4 ; if Len1=0 then Len1 := 4; Start := 1; Pre := False; for i := 1 to Level do begin s := Copy(str,Start,Len1); s1 := FourNumToChnNum(s,' 拾佰仟',Pre); // 注意有两个空格 if s1<>'' then Result := Result + s1 + Copy(ChnNum1,(Level-i)*2+1,2);; Start := Start + Len1; Len1 := 4; end; s1 := FourNumToChnNum(Copy(str,Len+2,2),'分角',Pre); if s1 = '' then s1 := '整'; Result := Result + s1 ; end;function RealToChnNum(realnum:real;Width:integer):string; var s:string; begin Str(realnum:Width:2,s); Result := StringToChnNum(Trim(s)); end;
以前曾经做过一个MIS,现根据我的记忆,写下来,可能有语法错误.如果有更好的算法,跟我打个招呼 int p=0; num数组的指针 int q=0; s_print数组的指针 int num[10]; 数据在此,num[0]为分,num[1]为角,num[2]为元..... boolean haveit; string numset[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"}; string moneyset[]={"","拾",:佰","仟"}; string s_print[20];要打印的,不过要反过来//只要不含"分"的均加上整.如贰百叁拾贰元叁角整,叁拾元整 haveit= ( num[0]==0 ); if haveit {s_print[0]="整"; p++;}//零钱单独处理 if ( num[0]!=0 ) { s_print[p]="分";p++; s_print[p]=numset[num[0]];p++; } if ( num[1]!=0 ) { s_print[p]="角";p++; s_print[p]=numset[num[1]];p++; } if ( ( num[q]!=0 )||( num[q+1]!=0 )||( num[q+2]!=0 )||( num[q+3]!=0 ) ) { s_print[p]="元"; p++; }if ( num[q]!=0 ) { // 不是个位 and 本位为0 and 前面还没有零 if ( ((q-2)%4!=0) && (num[q-1]==0) && (s_print[p-1]!="零") ) s_print[p]="零"; p++; s_print[p]=moneyset[(q-2)%4]; p++; //写拾,佰,千,万 s_print[p]=numset[num[q]]; p++; }//以下万级的内容与个级相同,亿级的也如此 if ( ( num[q]!=0 )||( num[q+1]!=0 )||( num[q+2]!=0 )||( num[q+3]!=0 ) ) { s_print[p]="万"; p++; }if ( num[q]!=0 ) { // 不是个位 and 本位为0 and 前面还没有零 if ( ((q-2)%4!=0) && (num[q-1]==0) && (s_print[p-1]!="零") ) s_print[p]="零"; p++; s_print[p]=moneyset[(q-2)%4]; p++; //写拾,佰,千,万 s_print[p]=numset[num[q]]; p++; }
function ChinaNum(X: Int64):string;
const
positions:Array[1..4] of string=( '仟','佰','拾','' );//四位一组的后缀
digits :Array[0..9] of string=( '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖');//大写定义
var
S,tmp: string;
Str: Array[1..4] of String;
Num: Array[1..4] of Integer;
ChinaNum: Array[1..4] of String;
I,J,K: Integer;
begin
if X>9999999999999999 then begin
Result:= '超出范围';
Exit;
end;
S:=format('%16u',[x]);//将数字转化为16位长度的字符串(不足添空格)
result:='';//返回值
ChinaNum[1]:='京';//单位
ChinaNum[2]:='亿';
ChinaNum[3]:='万';
ChinaNum[4]:='';
For I:= 1 to 4 do begin
Str[I]:= Copy(S, (I shl 2)-3,4);//字符串拷贝从(i shl 2)-3的位置开始的4个.i shl 2 表示左移2位
if Str[I]='0000' then begin
Str[I]:=' ';
Continue;
end
else begin
J:=Pos('000',Str[I]);
case J of
0: begin
J:=Pos('00', Str[I]);
Case J of
0:;
else Str[I,J]:=' ';
end;
end;
else begin
Str[I,J]:=' ';
Str[I,J+1]:=' ';
end;
end;
if Str[I,4]='0' then
Str[I,4]:=' ';
end;
end; For I:= 1 to 4 do begin
For J:= 4 Downto 1 do begin
case Str[I,J] of
'0': ChinaNum[I]:='零'+ChinaNum[I];
'1'..'9': begin
ChinaNum[I]:=Digits[Ord(Str[I,J])-48]+Positions[J]+ChinaNum[I];//Ord函数 取字符ASCII码
end;
else;
end;
end;
if (I=4)Or(Length(ChinaNum[I])>2) then begin
Result:= Result+ ChinaNum[I];
end;
end;
if Result='' then
Result:='零';
end;
const
ChnNum2='零壹贰叁肆伍陆柒捌玖';
var
i,j,Len:integer;
begin
Result := '';
Len := Length(str) ;
for i:=1 to Len do begin
j := Ord(str[i])-48;
if j=0 then
Pre := True
else begin
if Pre then
Result := Result + '零';
Result := Result + Copy(ChnNum2,j*2+1,2) + Trim(Copy(ChnNum,(Len - i) * 2+1,2));
Pre := False;
end;
end;
end;function StringToChnNum(str:string):string;
const
ChnNum1='圆万亿兆';
var
i,Len,Len1,Level,Start:integer;
s1,s:string;
Pre: Boolean;
begin
Result := '';
Len := Pos('.',str)-1;
Level := (Len + 3) div 4 ;
Len1 := Len mod 4 ;
if Len1=0 then
Len1 := 4;
Start := 1;
Pre := False;
for i := 1 to Level do begin
s := Copy(str,Start,Len1);
s1 := FourNumToChnNum(s,' 拾佰仟',Pre); // 注意有两个空格
if s1<>'' then
Result := Result + s1 + Copy(ChnNum1,(Level-i)*2+1,2);;
Start := Start + Len1;
Len1 := 4;
end;
s1 := FourNumToChnNum(Copy(str,Len+2,2),'分角',Pre);
if s1 = '' then
s1 := '整';
Result := Result + s1 ;
end;function RealToChnNum(realnum:real;Width:integer):string;
var
s:string;
begin
Str(realnum:Width:2,s);
Result := StringToChnNum(Trim(s));
end;
int p=0; num数组的指针
int q=0; s_print数组的指针
int num[10]; 数据在此,num[0]为分,num[1]为角,num[2]为元.....
boolean haveit;
string numset[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"};
string moneyset[]={"","拾",:佰","仟"};
string s_print[20];要打印的,不过要反过来//只要不含"分"的均加上整.如贰百叁拾贰元叁角整,叁拾元整
haveit= ( num[0]==0 );
if haveit {s_print[0]="整"; p++;}//零钱单独处理
if ( num[0]!=0 )
{
s_print[p]="分";p++;
s_print[p]=numset[num[0]];p++;
}
if ( num[1]!=0 )
{
s_print[p]="角";p++;
s_print[p]=numset[num[1]];p++;
}
if ( ( num[q]!=0 )||( num[q+1]!=0 )||( num[q+2]!=0 )||( num[q+3]!=0 ) )
{ s_print[p]="元"; p++; }if ( num[q]!=0 )
{
// 不是个位 and 本位为0 and 前面还没有零
if ( ((q-2)%4!=0) && (num[q-1]==0) && (s_print[p-1]!="零") )
s_print[p]="零"; p++; s_print[p]=moneyset[(q-2)%4]; p++; //写拾,佰,千,万
s_print[p]=numset[num[q]]; p++;
}//以下万级的内容与个级相同,亿级的也如此
if ( ( num[q]!=0 )||( num[q+1]!=0 )||( num[q+2]!=0 )||( num[q+3]!=0 ) )
{ s_print[p]="万"; p++; }if ( num[q]!=0 )
{
// 不是个位 and 本位为0 and 前面还没有零
if ( ((q-2)%4!=0) && (num[q-1]==0) && (s_print[p-1]!="零") )
s_print[p]="零"; p++; s_print[p]=moneyset[(q-2)%4]; p++; //写拾,佰,千,万
s_print[p]=numset[num[q]]; p++;
}