http://admincmd.blogspot.com/2007/08/php-convert-digit-to-thai-word.html
เอามาแปลงเป็น ruby ได้ผลออกมาแบบนี้
def convert(number)
txtnum1 = ['ศูนย์','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า','สิบ']
txtnum2 = ['','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน']
number.gsub!(",","")
number.gsub!(" ","")
number.gsub!("บาท","")
numbers = number.split(".")
if(numbers.length>2)
return "มีเครื่องหมาย '.' มากกว่า 1 ตัว"
end
strlen = numbers.first.length
convert = ""
0.upto(strlen-1) do |i|
n = numbers.first[i].chr.to_i
if (n.chr != "0")
if ( i == (strlen-1) and n == 1)
convert = convert + "เอ็ด"
elsif ( i == (strlen-2) and n == 2)
convert = convert + "ยี่"
elsif ( i == (strlen-2) and n == 1)
convert = convert + ""
else
puts "n = #{n.chr.to_i}"
convert = convert + txtnum1[n]
end
convert = convert + txtnum2[strlen-i-1]
end
end
convert = convert + "บาท"
if(numbers[1]=="0" or numbers[1]=="00" or numbers[1]=="")
convert = convert + "ถ้วน"
else
strlen = numbers[1].length
0.upto(strlen-1) do |i|
n = numbers.last[i].chr.to_i
if(n!=0)
if(i==(strlen-1) and n==1)
convert = convert + 'เอ็ด'
elsif(i==(strlen-2) and n==2)
convert = convert + 'ยี่'
elsif(i==(strlen-2) and n==1)
convert = convert + ''
else
convert = convert + txtnum1[n]
end
convert = convert + txtnum2[strlen-i-1]
end
end
convert = convert + 'สตางค์'
end
return convert
end
x = convert('5,121.10 บาท')
puts x
โปรแกรมสามารถ Refractor ได้อีก 2 ตลบ อันแรกคือ loop แทนที่จะวนด้วยตัวเลข ก็ใช้ string.each ได้เลย อีกอันคือโปรแกรมนี้ทำส่วนจำนวนเต็มกับทศนิยม คล้ายกันมาก น่าจะแยกออกมาเป็น function ได้