Welcome

首页 / 软件开发 / Delphi / 用Delphi将商品金额小写转大写

用Delphi将商品金额小写转大写2012-01-11 博客园 涂磊Function DecimalToChineseCurrency(Decimal: String): String; Var
s, t : TStringList;
i, ti : Integer;
tmps : String;
IsDecimal : Integer; Begin IsDecimal := 0;
ti := 0;
For i := 1 To Length(Decimal) Do
Begin
If Decimal[i] = "." Then
Begin Inc(IsDecimal);
ti := i;
End;
If Not (Decimal[i] In ["0".."9", "."]) Or (IsDecimal > 1) Then
Begin Result := Decimal;
Exit;
End;
End;
If ti = 0 Then //小数点的位置 Begin tmps := Decimal + ".";
ti := Length(tmps);
tmps := Decimal;
s := TStringList.Create;
t := TStringList.Create;
Try s.Delimiter := ",";
t.Delimiter := ",";
s.CommaText := "0=零,1=壹,2=贰,3=叁,4=肆,5=伍,6=陆,7=柒,8=捌,9=玖";
t.CommaText := "-1=分,0=角,1=元,2=拾,3=佰,4=仟,5=万,6=拾,7=佰,8=仟,9=亿,10=拾,11=佰,12=仟,13=兆,14=拾,15=佰,16=仟";
ti := Length(tmps) - ti;
tmps := StringReplace(tmps, ".", "", [rfReplaceAll]);
For i := 1 To Length(tmps) Do
Begin
If (tmps[Length(tmps) - i + 1] = "0") And (i Mod 4 = 1) Then
Result := t.Values[IntToStr(i - ti)] + Result
Else
If (tmps[Length(tmps) - i + 1] = "0") And (i Mod 4 <> 1) Then
Result := s.Values[tmps[Length(tmps) - i + 1]] + Result
Else
Result := s.Values[tmps[Length(tmps) - i + 1]] + t.Values[IntToStr(i - ti)] + Result;
End;
While Pos("零零", Result) > 0 Do
Result := StringReplace(Result, "零零", "零", [rfReplaceAll]);
If Pos("零兆", Result) > 0 Then Result := StringReplace(Result, "零兆", "兆", [rfReplaceAll]);
If Pos("零亿", Result) > 0 Then Result := StringReplace(Result, "零亿", "亿", [rfReplaceAll]);
If Pos("零万", Result) > 0 Then Result := StringReplace(Result, "零万", "万", [rfReplaceAll]);
If Pos("零元", Result) > 0 Then Result := StringReplace(Result, "零元", "元", [rfReplaceAll]);
If Pos("兆亿", Result) > 0 Then Result := StringReplace(Result, "兆亿", "兆", [rfReplaceAll]);
If Pos("亿万", Result) > 0 Then Result := StringReplace(Result, "亿万", "亿", [rfReplaceAll]);
If Pos("兆万", Result) > 0 Then Result := StringReplace(Result, "兆万", "兆", [rfReplaceAll]);
Finally t.Free;
s.Free;
End;
End;
{=====================================================}
Function ChineseCurrencyToDecimal(ChineseCurrency: String): String; Var
s : TStringList;
i, ti : Integer; Begin s := TStringList.Create;
Try s.Delimiter := ",";
s.CommaText := "元=.,零=0,壹=1,贰=2,叁=3,肆=4,伍=5,陆=6,柒=7,捌=8,玖=9";
ti := Length(ChineseCurrency) Div 2;
For i := ti Downto 1 Do
Result := s.Values[ChineseCurrency[i * 2 - 1] + ChineseCurrency[i * 2]] + Result;
Finally s.Free;
End;
End;