Skip navigation

Is it Ruby-way ala coding…

def terbilang(angka)
satuan = [”] + %w{satu dua tiga empat lima enam tujuh delapan sembilan sepuluh sebelas}.collect{|x|x+’ ‘}
triple = [[‘puluh’,10,20,100],[‘ratus’,100,200,1000],[‘ribu’,1000,2000,1000000],[‘juta’,1000000,1000000,1000000000],[‘milyar’,1000000000,1000000000,1000000000000]]
hasil = satuan[angka] if angka<12
hasil = terbilang(angka.divmod(10)[1]) +’belas ‘ if angka>=12 and angka<20
hasil = ‘seratus ‘+terbilang(angka-100) if angka>=100 and angka<200
hasil = ‘seribu ‘ +terbilang(angka-1000) if angka>=1000 and angka<2000
triple.each{|x|hasil=terbilang(angka.divmod(x[1])[0])+x[0]+’ ‘+terbilang(angka.divmod(x[1])[1]) if angka>=x[2] and angka<x[3]}
hasil
end

Advertisements

One Comment

  1. satuan = %w{satu dua tiga empat lima enam tujuh delapan sembilan sepuluh sebelas}.unshift(” “)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: