Number to word in Thai

Posted by Revolution Wed, 20 Feb 2008 09:40:00 GMT

อยู่ในภาคทดสอบ แบ่งเป็น 2 แบบคืออ่านธรรมดากับแบบค่าเงิน

ทดสอบได้ที่ num2word

class Number

  def initialize
    @unit = %w[ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า]
    @qtys = ["แสน", "หมื่น", "พัน", "ร้อย", "สิบ", "หน่วย"]
    @mils = "ล้าน"
    @zero = "ศูนย์"
    @twenty = "ยี่"
    @unitone = "เอ็ด"
  end

  def self.th_word(number)
    Number.new.to_s(number)
  end

  def self.th_currency(number)
    Number.new.to_s_currency(number)
  end


  def self.commify(number)
    (s=number.to_s;x=s.length;s).rjust(x+(3-(x%3))).gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
  end

  def to_s(value)
    if (number = value.to_s.split(".");number.length) >2 then return ("'.' more than 1") end
    re = []
    f = to_s_float(number[1]) if !number[1].nil?
    out = to_s_decimal(number[0])
    re[0] = f if !f.nil?
    re[1] = out
    if !f.nil? then re[2] = re[1] + "จุด" + re[0]   else re[2] = re[1] end
    return re.reverse
  end

  def to_s_currency(value)
    w = to_s(value).reverse
    f = value.to_s.gsub(/(\w*)+\./,"0.").to_f if !w[0].nil?
    w[0] = to_s_decimal((f * 100.0).round.to_s) if !f.nil?
    if !f.nil? then w[2] = w[1] + "จุด" + w[0]   else w[2] = w[1] end
    return w.reverse
  end

  def to_s_float(value)
    return float_wordify(value).flatten.compact.join(' ').gsub(/ ,/,',') 
  end

  def to_s_decimal(value)
    return quantify(value).flatten.compact.join(' ').gsub(/ ,/,',')
  end

private  

  def padded_groups(v)
    #make number to be 3 units per slot  by fill zero infront (x%3 =0)
    out = []
    padded = (s=v.to_s.gsub(/\D/i,"");x=s.length;s).rjust(x+(6-(x%6))).gsub(/ /,'0')
    padded.scan(/.{6}/)
  end

  def wordify(v)
    out = []
    @unitone = "หนึ่ง" if v.to_i == 1
    for cur in 0 .. v.length - 1
      if cur ==4 and v[cur] == '2'[0] 
        out << @twenty
      elsif cur ==4 and v[cur] == '1'[0] 
        out << "" 
      elsif cur ==5 and v[cur] == '1'[0] 
        out << @unitone
      else
      out << @unit[v[cur]-'0'[0]]
      end
    end
    return out
  end

  def float_wordify(v)
    out = []
    for cur in 0 .. v.length - 1
      out << @unit[v[cur]-'0'[0]]
    end
    out.pop while out.last == @zero
    return out
  end

  def quantify(v)
    v = padded_groups(v).reverse
    pos = v.length - 1
    out = []
    while pos >= 0
      if v[pos] == ','
        out << ','
        next
      end
      word = wordify(v[pos])
      for po in 0 .. word.length - 1
        if word[po] == @zero then next end
        if @qtys[po] != "หน่วย" then out << word[po] + @qtys[po] else out << word[po] end
      end
      out <<@mils if pos > 0
      pos -= 1
    end
    out.shift while out.first == @mils
    out << @zero if out.length.zero?
    return out
  end

end

Posted in  | Tags , , ,  | no comments | no trackbacks

Number to word

Posted by Revolution Tue, 12 Feb 2008 10:35:00 GMT

Source จาก http://sohne.net/articles/2006/04/30/convert-numbers-to-words/ เท่าที่แกะดูก็ไม่ยากแต่ไม่เข้าใจว่า หลายที่เงื่อนไขไม่ลงเลย สงสัยทำเผื่อไว้

Source from http://sohne.net/articles/2006/04/30/convert-numbers-to-words/ I have some confuse with this code, some condition will not may happen but i still be.

มีที่ดีกว่านี้ที่ http://www.deveiate.org/projects/Linguistics/wiki/English

class Number

  def self.to_words(number)
    Number.new.to_s(number)
  end

  def self.commify(number)
    (s=number.to_s;x=s.length;s).rjust(x+(3-(x%3))).gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
  end

  def initialize
    @unit = %w[zero one two three four five six seven eight nine]
    @teen = %w[ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen]
    @tens = %w[zero ten twenty thirty fourty fifty sixty seventy eighty ninety]
    @qtys = %w[hundred thousand million billion trillion quadrillion quintillion]
    @zero = ["zero"]
    @hundred = "hundred"
    @sepr = "and"
  end

  def to_s(number)
    out = quantify(number).flatten
    for x in 0 .. out.length - 1
      out[x] = nil if out[x] == @sepr && out[x+1] == @sepr
      out[x] = nil if out[x] == "," && out[x+1] == ","      
    end
    out.compact!
    out = @zero if out.length == 1 && out[0] == @sepr
    out.pop while out.last == @sepr
    out.shift while out.first == @sepr    
    out.join(' ').gsub(/ ,/,',')
  end

  private 

  def padded_groups(v)
    #make number to be 3 units per slot  by fill zero infront (x%3 =0)
    out = []
    padded = (s=v.to_s;x=s.length;s).rjust(x+(3-(x%3))).gsub(/ /,'0')
    padded.scan(/.{3}/)
  end

  def wordify(v)
    #Get 3 unit of slot and compare by ascii code, then word it out
    out = []
    zero = '0'[0]
    h, t, u = v[0] - zero, v[1] - zero, v[2] - zero
    if h != 0
      out << @unit[h]
      out << @hundred
    end
    #"and" word will add when hundred appear and unit appear 
    out << @sepr if h != 0 && (t != 0 || u != 0)
    out << @sepr if h == 0 && t == 0 && u != 0
    if t == 1
      out << @teen[u]
    else
      out << @tens[t] if t != 0
      out << @unit[u] if u != 0
    end
    return out
  end

  def quantify(v)
    #make ach 3 of each slot to be a word by calling wodify
    v = padded_groups(v).reverse
    cur = pos = v.length - 1
    out = []
    while pos >= 0
      if v[pos] == ','
        out << ','
        next
      end
      word = wordify(v[pos])
      if word[0] != nil
        out << word
        out << @qtys[cur] if cur != 0                
      else
        out << @sepr
      end
      cur -= 1
      pos -= 1
    end
    return out
  end

end

puts Number.to_words(1234567890)
puts Number.commify(1234567890)

ถ้าอยากรู้ว่า code ทำงานอย่างไรอ่านต่อข้างใน

Read more...

Posted in  | Tags  | 1 comment | no trackbacks

ชาบูชิ แหวะ Shabushi is vomit

Posted by Revolution Mon, 11 Feb 2008 10:31:00 GMT

เมื่อก่อนเคยกินบ่อยมาเมื่อ 5 ปีที่แล้วตอนยังเรียนอยู่ ไปกินที่ เดอะมอล์ บางกะปิ แต่ครั้งล่าสุดเมื่อปีที่แล้วที่ เดอะมอล์ บางกะปิ ของหมดหลายอย่างเหมือนรู้สึกว่าโดนโกง เลยคิดว่าคงไม่กินไปอีกนาน

5 years ago, I liked to have shabushi at The Mall Bangkapi. Last time that I ate that is 1 year ago, at that time many meats were away (out of shelf)

เมื่อวันอาทิตย์ที่ 10 กุมภาพันธ์ 2551 ไปกินชาบูชิ เพราะอยากลองกินน้ำซุปต้มยำดู ราคา 199++ ก็ประมา 234 บาทต่อคน เห็นของไม่หมด เลยลองดูที่ ซีคอน ด้านหน้าโลตัสชั้นหนึ่ง โคตรๆ สกปกเลย จานที่ใส่อาหารก็ล้างไม่สะอาด น้ำยังเลอะอยู่เลย กากก็ติดจาน ปลาซาบะก็ทอดไหม้ น้ำมันไม่เปลี่ยน โต๊ะก็เช็ดไม่เกลี้ยง หม้อมาชาไม่บวกเวลาให้ ที่สำคัญ ไอ้ของที่มันโฆษณาอยู่หน้าร้านมีไม่ถึง 70% ที่อยู่บนสายพาน ต้องขอจากคนที่อยู่ในซุ้ม เช่น เบคอน, หมูสไลด์, ไข่, ข้าวปันแซลม่อน, ไข่หวาน, ฯ

At Sunday 10 Feb 2008, I went to Seacon Square to have Shabushi because i want to try Tom Yam soup. Price is 199++, 234 Baht include vat and service charge. I tried it at 1 floor in front of Lotus. I conclude that Dirty!!!. Dish was surface with water and some food that stick on dish, Saba Fish is over fired, Pot and soup came too late (10 minutes), what are at ads board was not appear on conveyor i had to ask to waiter

เมื่อก่อนคิดว่าอีกไม่ไปกินอีกนาน แต่ตอนนี้คิดว่าคงไม่ไปกินอีกแล้ว

Before i think i will not take it for long time but this time i think that i will not take it again

ป.ล. ผมชอบ hotpot มากกว่านะ

P.S. I Love You

Posted in  | Tags ,  | 1 comment | no trackbacks

วันนี้เป็นวันจ่าย Today is shopping day

Posted by Revolution Tue, 05 Feb 2008 12:23:00 GMT

วันนี้เป็นวันจ่ายของตรุษจีนต้องซื้อของมาไหว้เจ้าและบรรพบุรุษมากมาย เช่น ไก่ เป็ด หมู พรุ่งนี้ไหว้

Today Chinese people buy thing to pray to angle and ancestor, such as duck, chicken, and pork

ต้องไปแล้ว

I have to go now

Posted in  | Tags ,  | no comments | no trackbacks

Ruby.NET เลิกพัฒนา

Posted by Revolution Tue, 05 Feb 2008 12:20:00 GMT

เขียนลง Blognone

Ruby เป็นภาษาที่ได้รับความสนใจไม่น้อย จนกระทั่งมีคนอยากให้ใช้งานกับ .NET ได้ด้วย ทางด๊อกเตอร์เคลลี่ ผู้พัฒนา Ruby.NET ได้ประกาศผ่านทางกูเกิ้ลกรุ๊ปแล้วว่าเขาจะเลิกพัฒนา Ruby.NET โดยเหตุผลว่า IronRuby นั่นประสพความสำเร็จและไปได้ไกลกว่า

เนื่องจาก Ruby.NET เองแรกเริ่มก็เป็นโครงงานที่แตกตัวออกมาจาก IronRuby ซึ่งถ้าจะทำ Ruby.NET ให้เข้าได้กับ DLR (Dynamic Language Runtime) ต้องทำการเปลี่ยนแปลงหลายอย่าง และด๊อกเตอร์เคลลี่มั่นใจว่ามันจะดีกว่า CLR (Common Language Runtime) ที่ใช้อยู่ในปัจจุบัน

ทางผู้พัฒนา Ruby.NET ก็มีทีท่าจะไปช่วยพัฒนา IronRuby อยู่ด้วย

ที่มา: Google Group

Posted in  | Tags ,  | no comments | no trackbacks