最精简的小写金额转大写的函数

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Edit1: TEdit;

    Edit2: TEdit;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



//方法一

function NumToChar(const n: Real): string;   //可以到万亿,并且可以随便扩大范围

  const cNum: WideString = '零壹贰叁肆伍陆柒捌玖--万仟佰拾亿仟佰拾万仟佰拾元角分';

        cCha:array[0..1, 0..12]of string =

        (( '零元','零拾','零佰','零仟','零万','零亿','亿万','零零零','零零','零万','零亿','亿万','零元'),

         ( '元','零','零','零','万','亿','亿','零','零','万','亿','亿','元'));

  var i : Integer;

      sNum,sTemp : WideString;

begin

  result :='';

  sNum := format('%15d',[round(n * 100)]);

  for i := 0 to 14 do

  begin

    stemp := copy(snum,i+1,1);

    if stemp=' ' then continue

    else result := result + cNum[strtoint(stemp)+1] + cNum[i+13];

  end;

  for i:= 0 to 12 do

  Result := StringReplace(Result, cCha[0,i], cCha[1,i], [rfReplaceAll]);

  if pos('零分',result)=0

    then Result := StringReplace(Result, '零角', '零', [rfReplaceAll])

    else Result := StringReplace(Result, '零角','整', [rfReplaceAll]);

  Result := StringReplace(Result, '零分','', [rfReplaceAll]);

end;



//方法二

function Changdx2(mmje: Double): String;

const s1: String = '零壹贰叁肆伍陆柒捌玖';

      s2: String = '分角元拾佰仟万拾佰仟亿拾佰仟万';



 function StrTran(const S, S1, S2: String): String;

  begin

    Result := StringReplace(S, S1, S2, [rfReplaceAll]);

  end;

 var s, dx: String;

     i, Len: Integer;

begin

if mmje < 0 then

  begin dx := '负';

        mmje := -mmje;

  end;

   s := Format('%.0f', [mmje*100]);

   Len := Length(s);

  for i := 1 to Len do

   dx := dx + Copy(s1, (Ord(s[i]) - Ord('0'))*2 + 1, 2) + Copy(s2, (Len - i)*2 + 1, 2);

   dx := StrTran(StrTran(StrTran(StrTran(StrTran(dx, '零仟', '零'), '零佰', '零'), '零拾', '零'), '零角', '零'), '零分', '整');

   dx := StrTran(StrTran(StrTran(StrTran(StrTran(dx, '零零', '零'), '零零', '零'), '零亿', '亿'), '零万', '万'), '零元', '元');

 if dx = '整' then Result := '零元整'

  else Result := StrTran(StrTran(dx, '亿万', '亿零'), '零整', '整');

end;









procedure TForm1.Button1Click(Sender: TObject);

begin

  ShowMessage(NumToChar(StrToInt(Edit1.text)));

  ShowMessage(Changdx2(StrToInt(Edit2.Text)));

end;



end.



你可能感兴趣的:(函数)