{
字符串替换
sData: 原始数据
sFind: 要替换的字符串
sReplace: 新字符串
}
function StringReplace(sData, sFind, sReplace: string): String;
var
pos1: integer;
s2,res: string;
begin
s2 := sData;
pos1 := pos(sFind,s2);
res := '';
while (pos1<>0) do begin
res := res + copy(s2,1,pos1-1) + sReplace;
s2 := copy(s2, pos1+length(sFind), length(s2) + 1 - (pos1+length(sFind)));
pos1 := pos(sFind,s2);
end;
res := res + s2;
Result := res;
end;
{
数字转中文(大写)
mmje: 数字
}
function NumberCn(mNumber: Real): String;
const
cPointCn = '点十百千万十百千亿十百千';
cNumberCn = '零一二三四五六七八九';
var
I, L, P: Integer;
S: string;
begin
Result := '';
if mNumber = 0 then begin
Result := cNumberCn[1];
Exit;
end;
S := FloatToStr(mNumber);
if Pos('.', S) <= 0 then S := S + '.';
P := Pos('.', S);
L := Length(S);
for I := 1 to L do
if P > I then
Result := Result + cNumberCn[StrToInt(S[I]) + 1] + cPointCn[P - I]
else if P = I then begin
Result := StringReplace(Result, '零十零', '零');
Result := StringReplace(Result, '零百零', '零');
Result := StringReplace(Result, '零千零', '零');
Result := StringReplace(Result, '零十', '零');
Result := StringReplace(Result, '零百', '零');
Result := StringReplace(Result, '零千', '零');
Result := StringReplace(Result, '零万', '万');
Result := StringReplace(Result, '零亿', '亿');
Result := StringReplace(Result, '亿万', '亿');
Result := StringReplace(Result, '零点', '点');
end else if P < I then
Result := Result + cNumberCn[StrToInt(S[I]) + 1];
if Result[Length(Result)] = cPointCn[1] then
Result := Copy(Result, 1, Length(Result) - 1);
if Result[1] = cPointCn[1] then Result := cNumberCn[1] + Result;
if (Length(Result) > 1) and (Result[2] = cPointCn[2]) and
(Result[1] = cNumberCn[2]) then
Delete(Result, 1, 1);
end; { NumberCn }
{
数字金额转中文(大写)
mmje: 数字金额
}
function MoneyToCharacter(mmje:real): String;
var
cNum: String = '零壹贰叁肆伍陆柒捌玖-万仟佰拾亿仟佰拾万仟佰拾元角分';
cCha0, cCha1: array[0..11] of string;
i: Integer;
sNum: String;
begin
cCha0[0] := '零仟';
cCha0[1] := '零佰';
cCha0[2] := '零拾';
cCha0[3] := '零零零';
cCha0[4] := '零零';
cCha0[5] := '零亿';
cCha0[6] := '零万';
cCha0[7] := '零元';
cCha0[8] := '亿万';
cCha0[9] := '零角';
cCha0[10] := '零分';
cCha0[11] := '零整';
cCha1[0] := '零';
cCha1[1] := '零';
cCha1[2] := '零';
cCha1[3] := '零';
cCha1[4] := '零';
cCha1[5] := '亿';
cCha1[6] := '万';
cCha1[7] := '元';
cCha1[8] := '亿';
cCha1[9] := '零';
cCha1[10] := '整';
cCha1[11] := '整';
Result := '';
if mmje < 0 then
begin
Result := '负';
mmje := -mmje;
end;
sNum := FormatFloat('0',mmje*100);
for i := 1 to Length(sNum) do
begin
Result := Result + cNum[ord(sNum[i])-47] + cNum[26-Length(sNum)+i];
end;
for i:= 0 to 11 do // 去掉多余的零
begin
Result := StringReplace(result, cCha0[i], cCha1[i]);
end;
end;
{
数字金额转中文(大写),去掉金额单位,并用指定分隔符分隔,以方便金额套打类功能
mmje: 金额
sp: 金额各段之间的分隔符,比如空格
}
function MoneyToCharacterWithoutUnit(mmje:real; sp:String ): String;
var
cNum: String = '亿万仟佰拾元角分';
i, p: Integer;
s, s1, s2, dest: String;
begin
dest := '';
s := MoneyToCharacter(mmje);
s := StringReplace(s, '零', '');
for i:=1 to Length(cNum) do begin
s2 := Copy(cNum, i, 1);
p := Pos(s2, s);
s1 := '';
if ( p > 0) then begin
s1 := Copy(s, 0, p-1);
s := Copy(s, p + 1, Length(s));
end;
if s1 = '' then
if dest='' then
s1 := '⊕' //第一个大写用特殊符号,表示起始
else
s1 := '零';
dest := dest + s1 + sp;
//dest := dest + s1 + s2;
end;
Result := dest;
end;