Советы по Delphi

Число строкой VI


Еще два решения конвертации денежной суммы на английском языке

Function HundredAtATime(TheAmount:Integer):String;
var
TheResult : String;Begin
TheResult := '';TheAmount := Abs(TheAmount);While TheAmount > 0 do BeginIf TheAmount >= 900 Then BeginTheResult := TheResult + 'Nine hundred ';TheAmount := TheAmount - 900;End;If TheAmount >= 800 Then BeginTheResult := TheResult + 'Eight hundred ';TheAmount := TheAmount - 800;End;If TheAmount >= 700 Then BeginTheResult := TheResult + 'Seven hundred ';TheAmount := TheAmount - 700;End;If TheAmount >= 600 Then BeginTheResult := TheResult + 'Six hundred ';TheAmount := TheAmount - 600;End;If TheAmount >= 500 Then BeginTheResult := TheResult + 'Five hundred ';TheAmount := TheAmount - 500;End;If TheAmount >= 400 Then BeginTheResult := TheResult + 'Four hundred ';TheAmount := TheAmount - 400;End;If TheAmount >= 300 Then BeginTheResult := TheResult + 'Three hundred ';TheAmount := TheAmount - 300;End;If TheAmount >= 200 Then BeginTheResult := TheResult + 'Two hundred ';TheAmount := TheAmount - 200;End;If TheAmount >= 100 Then BeginTheResult := TheResult + 'One hundred ';TheAmount := TheAmount - 100;End;If TheAmount >= 90 Then BeginTheResult := TheResult + 'Ninety ';TheAmount := TheAmount - 90;End;If TheAmount >= 80 Then BeginTheResult := TheResult + 'Eighty ';TheAmount := TheAmount - 80;End;If TheAmount >= 70 Then BeginTheResult := TheResult + 'Seventy ';TheAmount := TheAmount - 70;End;If TheAmount >= 60 Then BeginTheResult := TheResult + 'Sixty ';TheAmount := TheAmount - 60;End;If TheAmount >= 50 Then BeginTheResult := TheResult + 'Fifty ';TheAmount := TheAmount - 50;End;If TheAmount >= 40 Then BeginTheResult := TheResult + 'Fourty ';TheAmount := TheAmount - 40;End;If TheAmount >= 30 Then BeginTheResult := TheResult + 'Thirty ';TheAmount := TheAmount - 30;End;If TheAmount >= 20 Then BeginTheResult := TheResult + 'Twenty ';TheAmount := TheAmount - 20;End;If TheAmount >= 19 Then BeginTheResult := TheResult + 'Nineteen ';TheAmount := TheAmount - 19;End;If TheAmount >= 18 Then BeginTheResult := TheResult + 'Eighteen ';TheAmount := TheAmount - 18;End;If TheAmount >= 17 Then BeginTheResult := TheResult + 'Seventeen ';TheAmount := TheAmount - 17;End;If TheAmount >= 16 Then BeginTheResult := TheResult + 'Sixteen ';TheAmount := TheAmount - 16;End;If TheAmount >= 15 Then BeginTheResult := TheResult + 'Fifteen ';TheAmount := TheAmount - 15;End;If TheAmount >= 14 Then BeginTheResult := TheResult + 'Fourteen ';TheAmount := TheAmount - 14;End;If TheAmount >= 13 Then BeginTheResult := TheResult + 'Thirteen ';TheAmount := TheAmount - 13;End;If TheAmount >= 12 Then BeginTheResult := TheResult + 'Twelve ';TheAmount := TheAmount - 12;End;If TheAmount >= 11 Then BeginTheResult := TheResult + 'Eleven ';TheAmount := TheAmount - 11;End;If TheAmount >= 10 Then BeginTheResult := TheResult + 'Ten ';TheAmount := TheAmount - 10;End;If TheAmount >= 9 Then BeginTheResult := TheResult + 'Nine ';TheAmount := TheAmount - 9;End;If TheAmount >= 8 Then BeginTheResult := TheResult + 'Eight ';TheAmount := TheAmount - 8;End;If TheAmount >= 7 Then BeginTheResult := TheResult + 'Seven ';TheAmount := TheAmount - 7;End;If TheAmount >= 6 Then BeginTheResult := TheResult + 'Six ';TheAmount := TheAmount - 6;End;If TheAmount >= 5 Then BeginTheResult := TheResult + 'Five ';TheAmount := TheAmount - 5;End;If TheAmount >= 4 Then BeginTheResult := TheResult + 'Four ';TheAmount := TheAmount - 4;End;If TheAmount >= 3 Then BeginTheResult := TheResult + 'Three ';TheAmount := TheAmount - 3;End;If TheAmount >= 2 Then BeginTheResult := TheResult + 'Two ';TheAmount := TheAmount - 2;End;If TheAmount >= 1 Then BeginTheResult := TheResult + 'One ';TheAmount := TheAmount - 1;End;End;HundredAtATime := TheResult;End;

Function Real2CheckAmount(TheAmount:Real):String;
Var
IntVal : LongInt;TmpVal : Integer;TmpStr,RetVal : String;begin
TheAmount := Abs(TheAmount);
{ центы }TmpVal := Round(Frac(TheAmount) * 100);IntVal := Trunc(TheAmount);TmpStr := HundredAtATime(TmpVal);If TmpStr = '' Then TmpStr := 'Zero ';RetVal := TmpStr + 'cents';If IntVal > 0 Then RetVal := 'dollars and ' + RetVal;
{ сотни }TmpVal := Round(Frac((IntVal * 1.0) / 1000.0) * 1000);IntVal := Trunc((IntVal * 1.0) / 1000.0);TmpStr := HundredAtATime(TmpVal);RetVal := TmpStr + RetVal;
{ тысячи }TmpVal := Round(Frac((IntVal * 1.0) / 1000.0) * 1000);IntVal := Trunc((IntVal * 1.0) / 1000.0);TmpStr := HundredAtATime(TmpVal);If TmpStr <> '' ThenRetVal := TmpStr + 'Thousand ' + RetVal;
{ миллионы }TmpVal := Round(Frac((IntVal * 1.0) / 1000.0) * 1000);IntVal := Trunc((IntVal * 1.0) / 1000.0);TmpStr := HundredAtATime(TmpVal);If TmpStr <> '' ThenRetVal := TmpStr + 'Million ' + RetVal;
{ миллиарды }TmpVal := Round(Frac((IntVal * 1.0) / 1000.0) * 1000);IntVal := Trunc((IntVal * 1.0) / 1000.0);TmpStr := HundredAtATime(TmpVal);If TmpStr <> '' ThenRetVal := TmpStr + 'Billion ' + RetVal;
Real2CheckAmount := RetVal;end;
<
Хммммм... вроде бы работает, но как все громоздко и неуклюже.... добавьте в код немного рекурсии и вы получите более элегантную программу..:)))

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;
type
TForm1 = class(TForm)num: TEdit;spell: TEdit;Button1: TButton;procedure Button1Click(Sender: TObject);private{ Private declarations }function trans9(num: integer): string;function trans19(num: integer): string;function trans99(num: integer): string;function IntToSpell(num: integer): string;public{ Public declarations }end;
var
Form1: TForm1;
implementation

{$R *.DFM}
function TForm1.IntToSpell(num: integer): string;
var
spell: string;hspell: string;hundred: string;thousand: string;tthousand: string;hthousand: string;million: string;begin
if num ≶ 10 thenspell := trans9(num);{endif}if (num < 20) and (num &gt 10) thenspell := trans19(num);{endif}if (((num < 100) and (num > 19)) or (num = 10)) thenbeginhspell := copy(IntToStr(num),1,1) + '0';spell := trans99(StrToInt(hspell));hspell := copy(IntToStr(num),2,1);spell := spell + ' ' + IntToSpell(StrToInt(hspell));end;
if (num < 1000) and (num > 100) thenbeginhspell := copy(IntToStr(num),1,1);hundred := IntToSpell(StrToInt(hspell));hspell := copy(IntToStr(num),2,2);hundred := hundred + ' hundred and ' + IntToSpell(StrToInt(hspell));spell := hundred;end;
if (num < 10000) and (num > 1000) thenbeginhspell := copy(IntToStr(num),1,1);thousand := IntToSpell(StrToInt(hspell));hspell := copy(IntToStr(num),2,3);thousand := thousand + ' thousand ' + IntToSpell(StrToInt(hspell));spell := thousand;end;
if (num < 100000) and (num > 10000) thenbeginhspell := copy(IntToStr(num),1,2);tthousand := IntToSpell(StrToInt(hspell));hspell := copy(IntToStr(num),3,3);tthousand := tthousand + ' thousand ' + IntToSpell(StrToInt(hspell));spell := tthousand;end;
if (num < 1000000) and (num > 100000) thenbeginhspell := copy(IntToStr(num),1,3);hthousand := IntToSpell(StrToInt(hspell));hspell := copy(IntToStr(num),4,3);hthousand := hthousand + ' thousand and ' +IntToSpell(StrToInt(hspell));
spell := hthousand;end;
if (num < 10000000) and (num > 1000000) thenbeginhspell := copy(IntToStr(num),1,1);million := IntToSpell(StrToInt(hspell));hspell := copy(IntToStr(num),2,6);million := million + ' million and ' + IntToSpell(StrToInt(hspell));spell := million;end;
IntToSpell := spell;
end;

function TForm1.trans99(num: integer): string;
var
spell: string;begin
case
num of10 : spell := 'ten';20 : spell := 'twenty';30 : spell := 'thirty';40 : spell := 'fourty';50 : spell := 'fifty';60 : spell := 'sixty';70 : spell := 'seventy';80 : spell := 'eighty';90 : spell := 'ninty';end;trans99 := spell;end;
function TForm1.trans19(num: integer): string;
var
spell: string;begin
case
num of11 : spell := 'eleven';12 : spell := 'twelve';13 : spell := 'thirteen';14 : spell := 'fourteen';15 : spell := 'fifteen';16 : spell := 'sixteen';17 : spell := 'seventeen';18 : spell := 'eighteen';19 : spell := 'nineteen';end;trans19 := spell;end;
function TForm1.trans9(num: integer): string;
var
spell : string;begin
case
num of1 : spell := 'one';2 : spell := 'two';3 : spell := 'three';4 : spell := 'four';5 : spell := 'five';6 : spell := 'six';7 : spell := 'seven';8 : spell := 'eight';9 : spell := 'nine';end;trans9 := spell;end;
procedure TForm1.Button1Click(Sender: TObject);
var
numb: integer;begin
spell.text := IntToSpell(StrToInt(num.text));end;
[000256]


Содержание раздела