Sunday, September 28, 2014

Be Calculated (Terbilang)

implementation

function terbilang(dNumber: Extended): string;
const
 aNum: array[1..9] of String = ('satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan');
 aUnit: array[1..5] of String = ('trilyun', 'milyar', 'juta', 'ribu', '');
var
 iCount, iDigit1, iDigit2, iDigit3: Integer;
 sNum2Str, s3Digit, sWord: string;
begin

Result := '';
 if (dNumber=0) then Exit;
 sNum2Str:=Copy(Format('%18.2f', [dNumber]), 1, 15);
 for ICount:=1 to 5 do
 begin
 s3Digit:=Copy(sNum2Str, iCount*3-2, 3);
 if (StrToIntDef(s3Digit, 0)<>0) then
 begin
 sWord:='';
 iDigit1:=StrToIntDef(Copy(s3Digit, 1, 1), 0);
 iDigit2:=StrToIntDef(Copy(s3Digit, 2, 1), 0);
 iDigit3:=StrToIntDef(Copy(s3Digit, 3, 1), 0);
 case iDigit1 of
 2..9: sWord:=sWord+aNum[iDigit1]+' ratus ';
 1: sWord:=sWord+'seratus ';
 end; { case }

case iDigit2 of
 2..9: sWord:=sWord+aNum[iDigit2]+' puluh ';
 1: case iDigit3 of
 2..9: sWord:=sWord+aNum[iDigit3]+' belas ';
 1: sWord:=sWord+'sebelas ';
 0: sWord:=sWord+'sepuluh ';
 end; { case }
 end; { case }

if (iDigit2<>1) then
 case iDigit3 of
 2..9: sWord:=sWord + aNum[iDigit3] + ' ';
 1: if (iCount=4) and ((iDigit1+iDigit2)=0) then
 sWord:=sWord+'se'
 else
 sWord:=sWord+'satu ';
 end;
 Result:=Result+sWord+aUnit[iCount]+' ';
 end;
end;
while Result[Length(Result)]=' ' do
SetLength(Result, Length(Result)-1);
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
label1.caption:=terbilang(StrToFloatDef(edt1.Text,0));
end;

No comments:

Post a Comment