Terbilang

March 6, 2006 at 1:29 pm | Posted in Code Samples | 5 Comments

by: Kusnassriyanto

Perintah terbilang kelihatannya yang paling banyak diminta oleh anggota (baru) Delphindo. Meskipun sudah sejak lama kode terbilang ini berada di section file dari Delphindo, tetapi pertanyaan tentang hal ini masih saja sering muncul.
Berikut ini adalah kode terbilang yang di-upload oleh Bang Hans Gulo ke Delphindo.

// Pembilang
// Modified from Wien's code

const
  angka : array [0..9] of string = ('', 'satu ', 'dua ',
    'tiga ', 'empat ', 'lima ', 'enam ', 'tujuh ',
    'delapan ', 'sembilan ');
  level : array [0..7] of string = ('', 'ribu', 'juta',
    'milyar', 'trilyun','kuadriliun', 'aujubiliun',
    'banyakamitiliun');

// Input is in string.. so it does not have certain
// limitation make sure you have done input checking before
// passing it into this function

function pembilang(teks: string): string;
var
  x, grup3 : byte;
  hasil,
  processed,
  n, n1, n2, n3 : string;
begin
  hasil := '';
  // Pad text so it fits into chunks of three character
  for x := 1 to 3-(length(teks) mod 3)
    do insert ('0',teks,1);
  // Grup3 is the number of group of 3 character
  grup3 := length(Teks) div 3;
  for x := grup3-1 downto 0 do begin
    processed := copy(teks, 1, 3);
    teks := copy(teks, 4, length(teks)-3);
    n1 := ''; n2 := ''; n3 := '';
    if processed[1] = '1' then n1 := 'se'
      else n1 := angka[strtoint(processed[1])];
    if length(n1) > 0 then n1 := n1 + 'ratus ';
    n2 := angka[strtoint(processed[2])];
    if length(n2) > 0 then n2 := n2 + 'puluh ';
    n3 := angka[strtoint(processed[3])];
    if processed[2] = '1' then begin
      n2 := '';
      if processed [3] = '0' then n3 := 'sepuluh '
      else if processed [3] = '1' then n3 := 'sebelas '
      else n3 := angka[strtoint(processed[3])] + 'belas ';
    end;
    n := n1+n2+n3;
    // untuk seribu
    if (n = 'satu ') and (grup3 = 2) then n := 'se';
    hasil := hasil + n;
    if n <> '' then hasil := hasil + level[x]+' ';
  end;
  pembilang := hasil;
end;

5 Comments »

RSS feed for comments on this post. TrackBack URI

  1. aujubiliun” dan “banyakamitiliun” itu satuan darimana ya? ada2 aja deh. 😀

  2. kalo saya masukkan “1030” terbaca “seribu tiga puluh”, tetapi dengan “1001030” akan terbaca “satu juta satu ribu tiga puluh”, apakah yg benar bukan “satu juta seribu tiga puluh” ?

  3. Untuk 1001030, saya kira memang satu juta satu ribu tiga puluh.
    Tetapi bila diinginkan menjadi satu juta seribu tiga puluh, perubahannya cukup sederhana :
    Pada bagian akhir, yang tadinya

    // untuk seribu
    if (n = 'satu ') and (grup3 = 2) then n := 'se';

    diubah menjadi

    // untuk seribu
    if (n = 'satu ') and (x = 1) then n := 'se';

  4. Eh, sebenernya bukan saya yang upload lho. Kayanya nama saya yang muncul karena saya pernah reorganize folders di Files Section-nya Delphindo Yahoo!Groups.

    Credit should be given to original author (authors?), yaitu… ah lupa juga siapa (aja) 🙂

    • Wien….itu ya si erwien


Leave a reply to kusnassriyanto Cancel reply

Blog at WordPress.com.
Entries and comments feeds.