def number_to_tw_phone(phone, cellphone=false)
    raise "Phone number error!" if phone.size < 9 || phone.size > 10 || (phone.size < 10 if cellphone)
    no = phone.split("")
    return %{#{no[0..3].join}-#{no[4..6]}-#{no[7..9]}} if cellphone
    return %{(#{no[0..2].join})#{no[3..5]}-#{no[6..8].join}} if no[0..2].join == "089" # Tai-dong
    return %{(#{no[0..1].join})#{no[2..4]}-#{no[5..8].join}} if phone.size == 9
    return %{(#{no[0..1].join})#{no[2..5]}-#{no[6..9].join}} if phone.size == 10
  end
  puts number_to_tw_phone("0987654321", true) #=> 0987-654-321
  puts number_to_tw_phone("0234567890")         #=> (02)3456-7890
  puts number_to_tw_phone("087654321")           #=> (08)765-4321
  puts number_to_tw_phone("089876543")           #=> (089)876-543 #=> 這是台東的電話

創作者介紹

卐只有兩面的盒子卍

hechian 發表在 痞客邦 PIXNET 留言(0) 人氣()