13

Rubyで数字を単語に変換する方法は?

私はどこかに宝石があることを知っています。gemなしで実装しようとしています。整数の英語の単語に数字が必要なだけです。これを見つけましたが、非常に面倒です。よりクリーンで読みやすいソリューションを実装する方法について何か考えがある場合は、共有してください。

http://raveendran.wordpress.com/2009/05/29/ruby-convert-number-to-english-word/

これが私が取り組んできたことです。しかし、スケールの実装に問題があります。コードはまだ混乱しています。適切に機能するようになったら、もっと読みやすくしたいと思っています。

   class Numberswords
    def in_words(n)

    words_hash = {0=>"zero",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six",7=>"seven",8=>"eight",9=>"nine",
                    10=>"ten",11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen",
                     17=>"seventeen", 18=>"eighteen",19=>"nineteen",
                    20=>"twenty",30=>"thirty",40=>"forty",50=>"fifty",60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety"}

     scale = [000=>"",1000=>"thousand",1000000=>" million",1000000000=>" billion",1000000000000=>" trillion", 1000000000000000=>" quadrillion"]


    if words_hash.has_key?(n) 
      words_hash[n]

      #still working on this middle part. Anything above 999 will not work
     elsif n>= 1000  
     print  n.to_s.scan(/.{1,3}/) do |number|
            print number
      end



      #print value = n.to_s.reverse.scan(/.{1,3}/).inject([]) { |first_part,second_part| first_part << (second_part == "000" ? "" : second_part.reverse.to_i.in_words) }
      #(value.each_with_index.map { |first_part,second_part| first_part == "" ? "" : first_part + scale[second_part] }-[""]).reverse.join(" ")

    elsif n <= 99
       return [words_hash[n - n%10],words_hash[n%10]].join(" ")
    else
      words_hash.merge!({ 100=>"hundred" })
      ([(n%100 < 20 ? n%100 : n.to_s[2].to_i), n.to_s[1].to_i*10, 100, n.to_s[0].to_i]-[0]-[10])
        .reverse.map { |num| words_hash[num] }.join(" ")
    end
  end
end

#test code
test = Numberswords.new
 print test.in_words(200)
4

8 に答える 8

22

これに対する私の見解

def in_words(int)
  numbers_to_name = {
      1000000 => "million",
      1000 => "thousand",
      100 => "hundred",
      90 => "ninety",
      80 => "eighty",
      70 => "seventy",
      60 => "sixty",
      50 => "fifty",
      40 => "forty",
      30 => "thirty",
      20 => "twenty",
      19=>"nineteen",
      18=>"eighteen",
      17=>"seventeen", 
      16=>"sixteen",
      15=>"fifteen",
      14=>"fourteen",
      13=>"thirteen",              
      12=>"twelve",
      11 => "eleven",
      10 => "ten",
      9 => "nine",
      8 => "eight",
      7 => "seven",
      6 => "six",
      5 => "five",
      4 => "four",
      3 => "three",
      2 => "two",
      1 => "one"
    }
  str = ""
  numbers_to_name.each do |num, name|
    if int == 0
      return str
    elsif int.to_s.length == 1 && int/num > 0
      return str + "#{name}"      
    elsif int < 100 && int/num > 0
      return str + "#{name}" if int%num == 0
      return str + "#{name} " + in_words(int%num)
    elsif int/num > 0
      return str + in_words(int/num) + " #{name} " + in_words(int%num)
    end
  end
end



puts in_words(4) == "four"
puts in_words(27) == "twenty seven"
puts in_words(102) == "one hundred two"
puts in_words(38_079) == "thirty eight thousand seventy nine"
puts in_words(82102713) == "eighty two million one hundred two thousand seven hundred thirteen"
于 2014-10-06T16:22:59.553 に答える
8

簡単な答えはhumanizegemを使用すると、目的の出力が得られます

直接インストールする

gem install humanize

または、Gemfile に追加します。

gem 'humanize'

そして、あなたはそれを使うことができます

require 'humanize'

1.humanize       #=> 'one'
345.humanize     #=> 'three hundred and forty-five'
1723323.humanize #=> 'one million, seven hundred and twenty-three thousand, three hundred and twenty-three'

これをレールで使用している場合は、これを直接使用できます

注:以下のコメントでsrenが述べたように。によって提供されるヒューマナイズメソッドはActiveSupport、gem とは異なります。humanize

于 2015-11-24T12:19:26.800 に答える
1

お探しのものがわかりましたので、StackOverflow の投稿「Number to English Word Conversion Rails 」をご覧ください。

ここに要約があります:

いいえ、関数を自分で作成する必要があります。あなたが望むものに最も近いのは number_to_human ですが、それは 1 を One に変換しません。

参考になるかもしれないいくつかの URL を次に示します。

http://codesnippets.joyent.com/posts/show/447 http://raveendran.wordpress.com/2009/05/29/ruby-convert-number-to-english-word/ http://deveiate.org /プロジェクト/言語学/

于 2013-10-18T08:32:24.847 に答える
0

これがうまくいくかどうかはわかりません。メソッドはこのように呼び出すことができます。

n2w(33123) {|i| puts i unless i.to_s.empty?}

方法は次のとおりです (完全にはテストしていません。100 万まで動作すると思います。コードは醜く、リファクタリングの余地がたくさんあります。)

def n2w(n)
  words_hash = {0=>"zero",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six",7=>"seven",8=>"eight",9=>"nine",
                    10=>"ten",11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen",
                     17=>"seventeen", 18=>"eighteen",19=>"nineteen",
                    20=>"twenty",30=>"thirty",40=>"forty",50=>"fifty",60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety"}
  scale = {3=>"hundred",4 =>"thousand",6=>"million",9=>"billion"}

  if words_hash.has_key?n
    yield words_hash[n] 
  else
    ns = n.to_s.split(//)
      while ns.size > 0      
        if ns.size == 2
            yield("and")
            yield words_hash[(ns.join.to_i) - (ns.join.to_i)%10]            
            ns.shift
        end
        if ns.size > 4
          yield(words_hash[(ns[0,2].join.to_i) - (ns[0,2].join.to_i) % 10])
        else
          yield(words_hash[ns[0].to_i]) 
        end
        yield(scale[ns.size])
        ns.shift
      end
    end
end
于 2013-10-18T11:20:38.080 に答える