카테고리 없음

[일반/컴포넌트] 숫자를 영문 표기로 바꾸기

정보모음1 2023. 9. 13. 07:22
반응형
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function Amount(N: Longint): String;
const
  Units: array[0..9] of String = ('', 'One', 'Two', 'Three', 'Four',
                                  'Five', 'Six', 'Seven', 'Eight', 'Nine');
  Teens: array[0..9] of String = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen',
                                  'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
  Tens: array[0..9] of String = ('', 'Ten', 'Twenty', 'Thirty', 'Forty',
                                 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
  Higher: array[0..3] of String = ('', 'Thousand', 'Million', 'Billion');
  Level: Integer = 0;
begin
  case N of
    0..9 : Result := Result + Units[N];
    10..19 : Result := Result + Teens[N - 10];
    20..99 : Result := Result + Tens[N div 10] + Amount(N mod 10);
    100..999 : Result := Result + Units[N div 100] + ' Hundred' + Amount(N mod 100);
    else
    begin
      inc(Level);
      Result := Result + Amount(N div 1000) + Higher[Level] + Amount(N mod 1000);
      dec(Level);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Label1.caption := Amount(Trunc(StrToFloat(Edit1.Text)));
  except
    on EConvertError do
      Label1.caption := '정확한 숫자를 입력하세요';
  end;
end;

end.
반응형