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

วันนี้วันส่งเจ้ากลับขึ้นสวรรค์, Today all angels back to heven

Posted by Revolution Thu, 31 Jan 2008 10:18:00 GMT

สำหรับธรรมเนียมจีนแล้ววันนี้ถือว่าเป็นวันส่งเจ้ากลับขึ้นสู่สวรรค์เพื่อไปรายงานเง็กเซียนฮ้องเต้เกี่ยวกับความดีความชั่วของมนุษย์ เรียกว่าวัน ซิ้งเจี่ยที ก็จะต้องมีของมาไหว้หร้อมกระดาษเงินกระดาษทอง 13 ใบ (เพราะปีนึงมี 12 เดือนแต่บางปีมี 13 เดือน (เขาว่ามาอย่างงั้น)

For Chinese tradition, today is the day that all angels go back to heaven and report human’s virtue or sin to Angel Emperor. it’s usually call “Sing Jian Ti” in chinese. People pay respect and make a spirit offering to angel with 13 gold papers (Because a year has 12 month and some year have 13 month)

หลังจากนี้อีก 7 วันก็ตรุษจีนแล้วครับ

After this 7 days is chinese new year

Posted in  | Tags ,  | no comments | no trackbacks

วันนี้มีแต่เรื่องตื่นเต้น So excite Day

Posted by Revolution Mon, 28 Jan 2008 11:17:00 GMT

แต่เช้าเลยโรงงานผลิตของใช้เด็กโทรมาว่ามีการยักยอกกันจะขอมาถ่ายรูปของที่บ้านเป็นหลักฐาน เวรกรรมไม่ให้หรอกนะ

Early morning, Supplier call tom my home, they siad that there is corruption inside factory and they want police take a photo goods in my home. Suck no way.

ซัก 10 โมงกว่าๆไฟไหม้แถวบ้าน เมื่อไม่นานมานี้เพิ่งไฟไหม้สายไฟเสาไฟฟ้าไปเอง (การไฟฟ้าจะรับผิดชอบอะไรไหมเนี่ย) แต่ครั้งนี้ไหม้ร้านเฟอร์นิเจอร์ชั้นสอง ไม่แรงมากเพราะสถานีดับเพลิงอยู่ใกล้ แต่ก็ไหม้สินค้าน่าเสียดาย แต่เวรกรรมไม่มีเครื่องดับเพลิงในร้าน

Around 10 a.m., fire near my home, furniture shop’s second floor, lucky that fire station is near, it does not much damage. Just 2 week ago, it was fire a electricity power cable. No respond from Metropolitan Electricity Authorization

ล่าสุดเมื่อกี้ประมาณ 5 โมงกว่าๆรถกะบะพวงกะบะเปล่า คาดว่าวิ่งมาด้วยความเร็ว 40 กม/ขั่วโมง ขึ้นไป กะบะเปล่าหลุดออกจากตัวรถวิ่งมาชนเสาข้างซอย เล่นเอาบาทวิถีกระจุย ดีนะที่มันผ่านหน้าบ้านเราไป เพราะห่างซอยแค่หลังเดียว

Latest, 5 p.m. pickup truck with empty tray ran with 40 k.m./hr speed. Oh, it flee from truck and ran to hit pillar. Fortunately it ran pass my home, because previous home is mine.

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

BarcampBangkok winter2008

Posted by Revolution Mon, 28 Jan 2008 08:01:00 GMT

Great Great Barcamp, that’s the first barcamp in Thailand. First of all I would like to thank all of organizer and sponsor that help to establish barcamp.

สุดยอดๆบาร์แคมป์ ก่อนอื่ขอขอบคุณ ผู้จัดงานและผู้สนบบสุนที่จัดงานนี้ขึ้น

There are many topics that I would like to attend but my limitation of learning and understanding make me confused. So there is some topics i really like to attend.

มีหลายเรื่องเลยที่อยากเข้าไปฟัง แต่ด้วยสมองอันน้อยนิดแล้วก็เริ่มเรียนรู้ได้ช้าแล้วทำให้ถ้าเข้าไปฟังหลายๆอันจะสับสน แต่ก็มีบางเรื่องที่อยากรู้จริงๆ

  1. php framework by Hoffman ทำโดยคนไทยโดย Ford AntiTrust
  2. balance score card for small and medium it business(th/en):bunthid
  3. introduction to agile development overview of this very useful process(en):ben scherrey
  4. Localisation Dr.Rider
  5. seaside (beyond rails) (-) : p’pok
  6. yoda introduces to haskell (en): kirit
  7. multi threaded javascript (en): kirit
  8. System analysis and design(th/en) : ja
  9. so on..

Some session conflict each other and so I have to choose some or see 2 topics same time.

บางอันมันเวลาทับกัน ก็เลยต้องเลือกหรือถ้าห้อติดกันก็ต้องสลับดูพร้อมกันสองเรื่อง

For me, I shared my experience on topic “Ruby Hosting on Shared host” which is my core business and i also provide presentation for Download.

สำหรับผมไปแชร์เรื่องการทำ Ruby hosting on Shared Host ซึ่งเป็นหัวใจหลักในการทำธุรกิจของผมเลย ดาว์นโหลดได้ที่นี่

By the way, there is some point to improve
  • time – too short, just present, no time for discuss
  • Place – it’s ok but room is not big enough

งานครั้งนี้ผมยังได้เจออีกหลายๆคนที่ไม่เคยเจอตัวเป็นๆ หลายคนเช่น น้องรักโป้ง , ปั้นเหน่ง , P’Sugree ,P’Keng , เดียร์ , molecularck , Ford AntiTrust และอีกหลายๆคน

Barcamp on the beach will be happen soon I will not miss.

บาร์แคมป์ริมทะเลไม่พลาดแน่ๆ

People who also blog for barcampbangkok

Posted in  | Tags ,  | 1 comment | no trackbacks

คำแนะนำเมื่อต้องการพิพม์สีเยอะ

Posted by Revolution Wed, 23 Jan 2008 15:37:00 GMT

เนื่องจากจะทำปกสมุดไปแจกในงาน barcamp แต่เพิ่งรู้ตัวว่าหมึกสีจะหมด เลยลองถามเพื่อนๆดูว่าใครมีหมึกแบบ inktank มั่งได้คำตอบมา
  • ก็ซื้อ printer 2000 บาท เอามาพิมพ์เลยสิ ตกแผ่นละ 2 บาทเอง
  • ไป print บ้านเพื่อนครับพี่ ให้มันจ่าย (กูจะไป print บ้านมึงแหละแส๊ด)
  • Double A shop ครับ ไม่ผมไม่รู้ราคา

กวนจริงๆ สรุปว่าเอาหมึกไปเติม แล้วมาปริ๊น ราคาคงถูกที่สุดแล้ว

Posted in  | Tags  | no comments | no trackbacks

เปลี่ยน theme

Posted by Revolution Thu, 17 Jan 2008 07:20:00 GMT

ไหนๆก็ update เป็น typo 5.0.2 แล้วก็เลยลองเปลี่ยน theme เพราะ theme เห่ามีปัญหาไม่ยอมเว้น space กับภาษาไทย ก็เลยลองหาดูจาก typo theme เลยได้ theme eggdrop สีส้มเขียวอ่อนๆ ดูสบายตาดี ลูเล่นน่ารัก

แต่ใจอยากได้ origami แต่ค่อนข้างมี Bug หลาย feature ใม่ได้ ขี้เกียจแก้ด้วย เลยตกลงปลงใจที่ Theme eggdrop

ถ้าจะแก้ก็ไปแก้ที่ css ไม่รู้ว่าใช้รูปเยอะแค่ไหน

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

Older posts: 1 2 3 4 5 ... 11