⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 changermb.txt

📁 大量Delphi开发资料
💻 TXT
字号:
unit uChange;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    edtIn: TEdit;
    edtOut: TEdit;
    Bevel1: TBevel;
    btnChange: TBitBtn;
    procedure edtInKeyPress(Sender: TObject; var Key: Char);
    procedure edtInChange(Sender: TObject);
    procedure btnChangeClick(Sender: TObject);
  private
    { Private declarations }
    Function ChangeRMB(Const LowerMoney:String):String;    //核心作用
    Function isLegal(Const LowerMoney:String):Boolean;     //检测数是否合法
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.edtInKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key < '0') or (Key > '9') then   //只可输入数字
    if (Key <> '.') then
      Key := Chr(0);

  if Key = '.' then                    //只可输入一个小数点
    if StrRScan(PChar(edtIn.Text),'.') <> nil then
      Key := Chr(0);

end;

procedure TForm1.edtInChange(Sender: TObject);
begin
  if edtIn.Text = '' then
    btnChange.Enabled := False
  else btnChange.Enabled := True;
end;

procedure TForm1.btnChangeClick(Sender: TObject);
begin
  edtOut.Text := '';
  if isLegal(edtIn.Text) then           //判断字串是否为合法的数字
    edtOut.Text := ChangeRMB(edtIn.Text)
  else begin
    Application.MessageBox('一个数不能以 0 开头,请重新输入!','不合法的数!');
    edtIn.Clear;
    edtIn.SetFocus;
  end;
  btnChange.Enabled := False;
end;

function TForm1.ChangeRMB(const LowerMoney: String): String;
var
  NumStr:Array[1..10] of String;               //汉字大写的数字
  Sign:Array[1..12] of String;                 //汉字大写的单位
  IntPart ,                                    //整数部分
  RealPart ,                                   //小数部分
  UpperMoney:String;                           //存放大写人民币字符
  i , j:Integer;                               //循环变量
begin
  //开始初始化数组
  NumStr[1] := '零';  NumStr[2] := '壹';  NumStr[3] := '贰';
  NumStr[4] := '叁';  NumStr[5] := '肆';  NumStr[6] := '伍';
  NumStr[7] := '陆';  NumStr[8] := '柒';  NumStr[9] := '捌';
  NumStr[10] := '玖';
  Sign[1] := '圆'; Sign[2] := '拾'; Sign[3] := '佰'; Sign[4] := '仟';
  Sign[5] := '万'; Sign[6] := '拾'; Sign[7] := '佰'; Sign[8] := '仟';
  Sign[9] := '亿'; Sign[10] := '拾'; Sign[11] := '佰'; Sign[12] := '仟';
  //初始化数组结束,开始初始化字符串
  IntPart := '';  RealPart := '';  UpperMoney := '';

  //初始化字符串结束,开始给字符串赋值
  IntPart := FloatToStr(Int(StrToFloat(LowerMoney)));
  RealPart := FloatToStr(Frac(StrToFloat(LowerMoney)));
  //字符串赋值结束,开始数字和汉字转换
  j := 1;
  For i := Length(IntPart) downto 1 do begin
    UpperMoney := NumStr[StrToInt(IntPart[i]) + 1] + Sign[j] + UpperMoney;
    j := j + 1;
  end;
  //结束数字和汉字的转换,开始去掉多余的 0
  if Length(IntPart) >= 2 then                     //当是二位数时...
    if (IntPart[Length(IntPart)] = '0') then begin
      UpperMoney[Length(UpperMoney) - 2] := ' ';
      UpperMoney[Length(UpperMoney) - 3] := ' ';
    end;

  if Length(IntPart) >= 3 then                     //当是三位数时...
    if (IntPart[Length(IntPart) - 1] = '0') then begin
      UpperMoney[Length(UpperMoney) - 4] := ' ';
      UpperMoney[Length(UpperMoney) - 5] := ' ';
      if (IntPart[Length(IntPart)] = '0') then begin
        UpperMoney[Length(UpperMoney) - 6] := ' ';
        UpperMoney[Length(UpperMoney) - 7] := ' ';
      end;
    end;

  if Length(IntPart) >= 4 then                     //当是四位数时...
    if (IntPart[Length(IntPart) - 2] = '0') then begin
      UpperMoney[Length(UpperMoney) - 8] := ' ';
      UpperMoney[Length(UpperMoney) - 9] := ' ';
      if (IntPart[Length(IntPart) - 1] = '0') then begin
        UpperMoney[Length(UpperMoney) - 10] := ' ';
        UpperMoney[Length(UpperMoney) - 11] := ' ';
      end;
    end;

  if Length(IntPart) >= 5 then                     //当是五位数时...
    if (IntPart[Length(IntPart) - 3] = '0') then begin
      UpperMoney[Length(UpperMoney) - 12] := ' ';
      UpperMoney[Length(UpperMoney) - 13] := ' ';
      if (IntPart[Length(IntPart) - 2] = '0') then begin
        UpperMoney[Length(UpperMoney) - 14] := ' ';
        UpperMoney[Length(UpperMoney) - 15] := ' ';
      end;
    end;

  if Length(IntPart) >= 6 then                     //当是六位数时...
    if (IntPart[Length(IntPart) - 4] = '0') then begin
      UpperMoney[Length(UpperMoney) - 18] := ' ';
      UpperMoney[Length(UpperMoney) - 19] := ' ';
      if (IntPart[Length(IntPart) - 3] = '0') then begin
        UpperMoney[Length(UpperMoney) - 13] := ' ';
        UpperMoney[Length(UpperMoney) - 14] := ' ';
      end;
    end;

  if Length(IntPart) >= 7 then                     //当是七位数时...
    if (IntPart[Length(IntPart) - 5] = '0') then begin
      UpperMoney[Length(UpperMoney) - 20] := ' ';
      UpperMoney[Length(UpperMoney) - 21] := ' ';
      if (IntPart[Length(IntPart) - 4] = '0') then begin
        UpperMoney[Length(UpperMoney) - 18] := ' ';
        UpperMoney[Length(UpperMoney) - 19] := ' ';
        UpperMoney[Length(UpperMoney) - 22] := ' ';
        UpperMoney[Length(UpperMoney) - 23] := ' ';
      end;
    end;
  if Length(IntPart) >= 8 then                     //当是八位数时...
    if (IntPart[Length(IntPart) - 6] = '0') then begin
      UpperMoney[Length(UpperMoney) - 24] := ' ';
      UpperMoney[Length(UpperMoney) - 25] := ' ';
      if (IntPart[Length(IntPart) - 5] = '0') then begin
        UpperMoney[Length(UpperMoney) - 20] := ' ';
        UpperMoney[Length(UpperMoney) - 21] := ' ';
        UpperMoney[Length(UpperMoney) - 22] := ' ';
        UpperMoney[Length(UpperMoney) - 23] := ' ';
        if (IntPart[Length(IntPart) - 4] = '0') then begin
          UpperMoney[Length(UpperMoney) - 18] := ' ';
          UpperMoney[Length(UpperMoney) - 19] := ' ';
          UpperMoney[Length(UpperMoney) - 26] := ' ';
          UpperMoney[Length(UpperMoney) - 27] := ' ';
        end;
      end;
    end;

  if Length(IntPart) >= 9 then                     //当是九位数时...
    if (IntPart[Length(IntPart) - 7] = '0') then begin
      UpperMoney[Length(UpperMoney) - 28] := ' ';
      UpperMoney[Length(UpperMoney) - 29] := ' ';
      if (IntPart[Length(IntPart) - 6] = '0') then begin
        UpperMoney[Length(UpperMoney) - 24] := ' ';
        UpperMoney[Length(UpperMoney) - 25] := ' ';
        UpperMoney[Length(UpperMoney) - 26] := ' ';
        UpperMoney[Length(UpperMoney) - 27] := ' ';
        UpperMoney[Length(UpperMoney) - 28] := ' ';
        UpperMoney[Length(UpperMoney) - 29] := ' ';
        UpperMoney[Length(UpperMoney) - 30] := ' ';
        UpperMoney[Length(UpperMoney) - 31] := ' ';
      end;
    end;

  //结束去掉多余的 0 ,开始转换小数部分
  if Length(RealPart) = 1 then
    UpperMoney := UpperMoney + '整';

  if Length(RealPart) = 3 then begin
    if IntPart[Length(IntPart)] = '0' then
      UpperMoney := UpperMoney + '零';
    UpperMoney := UpperMoney + NumStr[StrToInt(RealPart[3]) + 1] + '角整';
  end;

  if Length(RealPart) >= 4 then begin
    if RealPart[3] = '0' then begin
      UpperMoney := UpperMoney + '零';
      if RealPart[4] = '0' then begin
        UpperMoney[Length(UpperMoney)] := ' ';
        UpperMoney[Length(UpperMoney) - 1] := ' ';
      end else
        UpperMoney := UpperMoney + NumStr[StrToInt(RealPart[4]) + 1] + '分';
    end;
    if RealPart[3] <> '0' then begin
      if IntPart[Length(IntPart)] = '0' then
        UpperMoney := UpperMoney + '零';
      UpperMoney := UpperMoney + NumStr[StrToInt(RealPart[3]) + 1] + '角';
      if RealPart[4] <> '0' then
        UpperMoney := UpperMoney + NumStr[StrToInt(RealPart[4]) + 1] + '分'
      else
        UpperMoney := UpperMoney + '整';
    end;
  end;

  if Length(IntPart) = 1 then for i := 1 to 6 do UpperMoney[i] := ' ';
  //小数部分转换结束,开始去掉多余的空格
  For i := 1 to Length(UpperMoney) do
    if UpperMoney[i] <> ' ' then
      Result := Result + UpperMoney[i] ;
  //空格去除完成 , 开始完善大写人民币字符串
end;

function TForm1.isLegal(const LowerMoney: String): Boolean;
begin
  Result := True;
  if (LowerMoney[1] = '0') and (LowerMoney[2] = '0') then
    Result := False;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -