metod แปลงตัวเลขเป็นอักษรแบบง่าย(เอง)เอามาฝากเพื่อเป็นประโยชน์

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1001 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1001 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

def self.num_to_text(num)

ntt=num.to_s ttn=” c=0 j=’0123456789’

while c<=(ntt.length-1) if ntt[c]==j[1] if c==(ntt.length-1)&& ntt.length>1 one=’เอ็ด’ else one=’หนึ่ง’ end ttn=ttn+one end

                            if ntt[c]==j[2]
                                if c==(ntt.length-2)&& ntt.length>1
                                  two='ยี่'
                                else
                                  two='สอง'
                                end
                                ttn=ttn+two
                             end

                            if ntt[c]==j[3]
                                ttn=ttn+'สาม'
                            end
                            if ntt[c]==j[4]
                                ttn=ttn+'สี่'
                            end
                            if ntt[c]==j[5]
                                ttn=ttn+'ห้า'
                            end
                            if ntt[c]==j[6]
                                ttn=ttn+'หก'
                            end
                            if ntt[c]==j[7]
                                ttn=ttn+'เจ็ด'
                            end
                            if ntt[c]==j[8]
                                ttn=ttn+'แปด'
                            end
                            if ntt[c]==j[9]
                                ttn=ttn+'เก้า'
                            end

                            if ntt.length-c-1==6 || ntt.length-c-1==12 || ntt.length-c-1==18 
                                ttn=ttn+'ล้าน'
                            end
                            if ntt[c]!=j[0]
                            if ntt.length-c-1==5 || ntt.length-c-1==11 || ntt.length-c-1==17
                                ttn=ttn+'แสน'
                            end
                            if ntt.length-c-1==4 || ntt.length-c-1==10 || ntt.length-c-1==16
                                ttn=ttn+'หมื่น'
                            end
                            if ntt.length-c-1==3 || ntt.length-c-1==9 || ntt.length-c-1==15
                                ttn=ttn+'พัน'
                            end
                            if ntt.length-c-1==2 || ntt.length-c-1==8 || ntt.length-c-1==14
                                ttn=ttn+'ร้อย'
                            end
                            if ntt.length-c-1==1 || ntt.length-c-1==7 || ntt.length-c-1==13
                                ttn=ttn+'สิบ'
                            end
                            end

c=c+1 end

return ttn end

แล้วอันนี้ใส่ comma ให้กับตัวเลข ทำเอง แบบ งง งง

def self.commas(x) str = x.to_s.reverse str.gsub!(/([0-9]{3})/,”\1,”) str.gsub(/,$/,”“).reverse end

ทำไม พี่ ไม่ โพสให้ไวกว่านี้ครับ ผมเขียนสายถึก ไปเรียบร้อยแล้วครับยาวเป็นพรืดเลยครับ ของผมขอฝากตัวด้วยนะครับ แถมคอมม่าให้อีกตัวครับครับ

code

  def num_to_t(num,x)
 num_r=''
 
 if num=='1'
     num_st='หนึ่ง'
 elsif num=='2'
   num_st='สอง'
 elsif num=='3'
   num_st='สาม'
 elsif num=='4'
   num_st='สี่'
 elsif num=='5'
   num_st='ห้า'
 elsif num=='6'
   num_st='หก'
 elsif num=='7'
   num_st='เจ็ด'
 elsif num=='8'
   num_st='แปด'
 elsif num=='9'
   num_st='เก้า'
 else
   num_st=''
 end
 
if num!='0'
 if (x=='1')||(x=='7')
 
   if num=='2'
     num_st='ยี่'
   end
   num_s='สิบ'
 elsif (x=='2')||(x=='8')
   num_s='ร้อย'
 elsif (x=='3')||(x=='9')
   num_s='พัน'
 elsif (x=='4')||(x=='10')
   num_s='หมื่น'
 elsif (x=='5')||(x=='11')
   num_s='แสน'
 elsif (x=='6')||(x=='12')
   num_s='ล้าน'
 else 
   num_s=''
 end
 
 if (((x=='1')||(x=='7'))&&(num=='1')) #เงื่อนไขในหลังสิบ
  num_r =  num_s
 else
  num_r = num_st + num_s
 end
 
end
 
 return num_r
end
 
def num_to_text(num)
  num_s=(num.to_f).to_s     
  num_l=num_s.length        
  num_spot=num_s.index('.') 
  b=num_s.to_s[0,num_spot]  
  s=num_s.to_s[num_spot+1,2]    
 
  num1=''
  n=(b.length)-1
  for i in 0..n
 
    tmp=b.to_s[i,1]
    puts tmp
    buf=num_to_t(tmp,(n-i).to_s)
 
    if n==0
      num1 << buf
    else
      if (i==n)||((n-i)==6)           
    if ('หนึ่ง'==buf)
          num1 << 'เอ็ด'
        elsif('หนึ่งล้าน'==buf)&&(n!=6)
      num1 << 'เอ็ดล้าน'
    else
          num1 << buf
        end
      else
        num1 << buf
      end
    end
 
  end
 
 if num1==''
   num1='ศูนย์'
 end
   num1 << 'บาท'
 
n=1
num2=''
  for i in 0..n
    tmp=s.to_s[i,1]
    puts tmp
    buf=num_to_t(tmp,(n-i).to_s)
 
    if n==0
      num2 << buf
    else
      if n==i
    if 'หนึ่ง'==buf
          num2 << 'เอ็ด'
        else
          num2 << buf
        end
      else
        num2 << buf
      end
    end
  end
 if num2==''
   num2='ถ้วน'
 else
   num2 << 'สตางค์'
 end
 
  return '---'+num1+num2+'---'
end
 
public
def commas(x)
  x       = x.to_f
  x       = sprintf("%.2f",x)
  x_spot  = x.index('.')    
  x_b     = x.to_s[0,x_spot]    
  x_a     = x.to_s[x_spot,3]  
 
  str = x_b.reverse
  str.gsub!(/([0-9]{3})/,"\\1,")
  str=str.gsub(/,$/,"").reverse
  str+x_a
end

post code สวยงามใช้ blockcode :-) http://blog.vee-u.com/2008/02/17/blockcode/

taiko_gogo's picture

เอามั่ง ๆ

class ThaiNumPronounciation
  TNUM = %W{หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า สิบ}
  TPOS = %W{ล้าน สิบ ร้อย พัน หมื่น แสน}
  SPOS = %W{เอ็ด ยี่}
  def initialize(value)
    @num=value.split(",").join("")
  end
  def toString()
    ot=[]
    @num.split("").reverse.each_with_index do |value,key|
      ot.push TPOS[key%6]
      unless value.to_i==1 and key%6==1
        unless key == @num.length-1 and value == 1.to_s
          ot.push( ((value==2.to_s and key % 6 ==1 ) or (value==1.to_s and key % 6 ==0 )) ? SPOS[value.to_i-1] : TNUM[value.to_i-1])
        else
          ot.push TNUM[value.to_i-1]
        end
      end
    end
    return ot.length==1 ? ot : ot.reverse[0...ot.length-1].join("")
  end
end
x=ThaiNumPronounciation.new("12343")
x.toString

ไอเดียคือ ไล่อ่านค่าจากหลังสุด โดนทู่ซี่ใส่ “ล้าน” เข้าไปที่หลักหน่วย เพื่อให้สามารถ ใช้ TPOS[key%6] ในการไล่ใส่ค่าประจำหน่วยได้ แล้วค่อยไปตัดล้านที่มันเกินมาตรงหลักหน่วยออก ที่เหลือก็ง่าย ๆ ไม่มีอะไร อ่านเข้ามา,เช็คตำแหน่ง แล้ว เก็บค่าคำอ่านที่เหมาะสม ไปเรื่อยๆ

ของคุณ taiko_gogo ดูสั้นง่าย เข้าใจดีครับ

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น