Spiga

Function Terbilang Delphi (Indonesia+English)

Semoga artikel ini bisa buat perbandingan dan menambah ilmu buat temen2 dan berguna untuk Aplikasi Keuangan seperti penulisan terbilang pada Kuitansi, Slip bukti pembayaran dan sebagainya. Component ini akan menerjemahkan bilangan yang Anda masukkan ke dalam terbilang dalam Bahasa Indonesia. Contoh. 125 akan diterjemahkan jadi "seratus dua puluh lima"

Begin
//Terbilang
class Function Terbilang.TerbilangEnglish(dblValue : Double) : String;
var
ones : array[0..9] of String;
teens : array[0..9] of String;
tens : array[0..9] of String;
thousands : array[0..4] of String;
i, nPosition, nDigit, bAllZeros : Integer;
strResult, strTemp, tmpBuff : String;

begin
ones[0] := 'ZERO';
ones[1] := 'ONE';
ones[2] := 'TWO';
ones[3] := 'THREE';
ones[4] := 'FOUR';
ones[5] := 'FIVE';
ones[6] := 'SIX';
ones[7] := 'SEVEN';
ones[8] := 'EIGHT';
ones[9] := 'NINE';

teens[0] := 'TEN';
teens[1] := 'ELEVEN';
teens[2] := 'TWELVE';
teens[3] := 'THIRTEEN';
teens[4] := 'FOURTEEN';
teens[5] := 'FIFTEEN';
teens[6] := 'SIXTEEN';
teens[7] := 'SEVENTEEN';
teens[8] := 'EIGHTEEN';
teens[9] := 'NINETEEN';

tens[0] := '';
tens[1] := ''; //TEN1

tens[2] := 'TWENTY';
tens[3] := 'THIRTY';
tens[4] := 'FORTY';
tens[5] := 'FIFTY';
tens[6] := 'SIXTY';
tens[7] := 'SEVENTY';
tens[8] := 'EIGHTY';
tens[9] := 'NINTY';

thousands[0] := '';
thousands[1] := 'THOUSAND';
thousands[2] := 'MILION';
thousands[3] := 'BILLION';
thousands[4] := 'TRILLION';

try

strResult := '';
strTemp := IntToStr(round(dblValue));
//Iterate through string
For i := Length(strTemp) downTo 1 do
begin

//Get value of this digit
nDigit := StrToInt(MidStr(strTemp, i, 1));
//Get column position
nPosition := (Length(strTemp) - i) + 1;
//Action depends on 1's, 10's or 100's column
//Select Case (nPosition Mod 3)
case (nPosition Mod 3) of

1 : //Case 1 //'1's position
begin
bAllZeros := 0;
if i = 1 Then

tmpBuff := ones[nDigit] + ' '
else
if MidStr(strTemp, i - 1, 1) = '1' Then

begin
tmpBuff := teens[nDigit] + ' ';
end
else
if nDigit > 0 Then

tmpBuff := ones[nDigit] + ' '
else
begin
//If next 10s & 100s columns are also
//zero, then don't show 'thousands'
bAllZeros := 1;
if i > 1 Then

begin
If MidStr(strTemp, i - 1, 1) <> '0' Then
bAllZeros := 0;
end;
If i > 2 Then

begin
If MidStr(strTemp, i - 2, 1) <> '0' Then
bAllZeros := 0;

End;
tmpBuff := '';
end;
If (bAllZeros = 0) and (nPosition > 1) Then

tmpBuff := tmpBuff + thousands[nPosition div 3] + ' ';

strResult := tmpBuff + strResult;

end;
2 : //Tens position
begin

if nDigit > 0 Then
//if MidStr(strTemp, i - 1, 1) <> '1' Then
strResult := tens[nDigit] + ' ' + strResult;

end;
0 : //Hundreds position

begin

if nDigit > 0 Then
strResult := ones[nDigit] + ' HUNDRED ' + strResult;

end;
end;
end;

Result := '( ' + strResult;

except

Result := '';
end;

end;

class Function Terbilang.TerbilangIndonesia(dblValue : Double) : String;
var

ones,one : array[0..9] of String;
teens : array[0..9] of String;
tens : array[0..9] of String;
thousands : array[0..4] of String;
i, nPosition, nDigit, bAllZeros : Integer;
strResult, strTemp, tmpBuff : String;

begin

ones[0] := 'NOL';
ones[1] := 'SATU';
ones[2] := 'DUA';
ones[3] := 'TIGA';
ones[4] := 'EMPAT';
ones[5] := 'LIMA';
ones[6] := 'ENAM';
ones[7] := 'TUJUH';
ones[8] := 'DELAPAN';
ones[9] := 'SEMBILAN';

one[1] := 'SE';

teens[0] := 'SEPULUH';
teens[1] := 'SEBELAS';
teens[2] := 'DUA BELAS';
teens[3] := 'TIGA BELAS';
teens[4] := 'EMPAT BELAS';
teens[5] := 'LIMA BELAS';
teens[6] := 'ENAM BELAS';
teens[7] := 'TUJUH BELAS';
teens[8] := 'DELAPAN BELAS';
teens[9] := 'SEMBILAN BELAS';

tens[0] := '';
tens[1] := '';
tens[2] := 'DUA PULUH';
tens[3] := 'TIGA PULUH';
tens[4] := 'EMPAT PULUH';
tens[5] := 'LIMA PULUH';
tens[6] := 'ENAM PULUH';
tens[7] := 'TUJUH PULUH';
tens[8] := 'DELAPAN PULUH';
tens[9] := 'SEMBILAN PULUH';

thousands[0] := '';
thousands[1] := 'RIBU ';
thousands[2] := 'JUTA ';
thousands[3] := 'MILIAR ';
thousands[4] := 'TRILIUN ';

try

strResult := '';
strTemp := FloatToStr(dblValue);
//Iterate through string
For i := Length(strTemp) downTo 1 do
begin

//Get value of this digit
nDigit := StrToInt(MidStr(strTemp, i, 1));
//Get column position
nPosition := (Length(strTemp) - i) + 1;
//Action depends on 1's, 10's or 100's column
//Select Case (nPosition Mod 3)
case (nPosition Mod 3) of

1 :
begin
//Case 1 //'1's position
bAllZeros := 0;
if i = 1 Then

tmpBuff := ones[nDigit] + ' '
else
if MidStr(strTemp, i - 1, 1) = '1' Then

begin
tmpBuff := teens[nDigit] + ' ';
end
else
if nDigit > 0 Then

tmpBuff := ones[nDigit] + ' '
else
begin
//If next 10s & 100s columns are also
//zero, then don't show 'thousands'
bAllZeros := 1;
if i > 1 Then

begin
If MidStr(strTemp, i - 1, 1) <> '0' Then
bAllZeros := 0;
end;
If i > 2 Then

begin
If MidStr(strTemp, i - 2, 1) <> '0' Then
bAllZeros := 0;

End;
tmpBuff := '';
end;
If (bAllZeros = 0) and (nPosition > 1) Then

tmpBuff := tmpBuff + thousands[nPosition div 3] + ' ';

strResult := tmpBuff + strResult;

end;
2 : //Tens position
begin

if nDigit > 0 Then
if nDigit = 1 Then

begin
end
else
strResult := tens[nDigit] + ' ' + strResult

end;
0 : //Hundreds position

begin
if nDigit > 0 Then
if nDigit = 1 Then

begin
if (nPosition mod 3) = 0 then
strResult := one[nDigit] + 'RATUS ' + strResult;
end

else
strResult := ones[nDigit] + ' RATUS ' + strResult;
end;
end;
end;

Result := '( ' + strResult;

except

Result := '';
end;

end;
//Terbilang
end;

Komentar :

Ada 2 komentar ke “Function Terbilang Delphi (Indonesia+English)”
Disamarkan said...
pada hari 

Cara menggunakan fungsinya , gmna mas?

maspry17 said...
pada hari 

Makasih mas..it work,

Post a Comment

Keluarkan uneg-unekmu sobat!! Jangan disimpan dalam hati, dan saya ucapkan terima kasih...