【问题标题】:Convert string numbers( in word format) to integer ruby将字符串数字(以 word 格式)转换为整数 ruby
【发布时间】:2012-11-26 12:16:24
【问题描述】:

如果您有一个字符串ten,是否可以在Ruby 中将其转换为整数10? (也许在轨道上?)

我很重视 tryruby.org 的开发人员,在他们的教程 here 中,它特别说“to_i 将事物转换为整数(数字)。我想知道他们为什么不说“to_i 将字符串转换为整数(数字。)”

哪些变量类型可以从它们的类型转换为整数?

【问题讨论】:

    标签: ruby


    【解决方案1】:

    我会怎么做。

    def n_to_s(int)
    
        set1 = ["","one","two","three","four","five","six","seven",
             "eight","nine","ten","eleven","twelve","thirteen",
             "fourteen","fifteen","sixteen","seventeen","eighteen",
             "nineteen"]
    
        set2 = ["","","twenty","thirty","forty","fifty","sixty",
             "seventy","eighty","ninety"]
    
        thousands = (int/1000)
        hundreds = ((int%1000) / 100)
        tens = ((int % 100) / 10)
        ones = int % 10
        string = ""
    
        string += set1[thousands] + " thousand " if thousands != 0 if thousands > 0
        string += set1[hundreds] + " hundred" if hundreds != 0
        string +=" and " if tens != 0 || ones != 0 
        string = string + set1[tens*10+ones] if tens < 2
        string += set2[tens]
        string = string + " " + set1[ones] if ones != 0     
        string << 'zero' if int == 0    
        p string
    end
    

    为了测试目的;

    n_to_s(rand(9999))
    

    【讨论】:

      【解决方案2】:

      查看this gem 以处理单词到数字的转换。

      来自自述文件:

      require 'numbers_in_words'
      require 'numbers_in_words/duck_punch'
      
      112.in_words
      #=> one hundred and twelve
      "Seventy million, five-hundred and fifty six thousand point eight nine three".in_numbers
      #=> 70556000.893
      

      【讨论】:

        【解决方案3】:

        由于String#to_i 只选择数字字符,它不会按照您想要的方式工作。可能有一些 Rails 方法与此相关,但它肯定不会有方法名称to_i,因为它的行为会与String#to_i 的初衷相冲突。

        不仅Stringsto_iNilClassTimeFloatRational(可能还有其他一些类)也可以。

        "3".to_i #=> 3
        "".to_i #=> 0
        nil.to_i #=> 0
        Time.now.to_i #=> 1353932622
        (3.0).to_i #=> 3
        Rational(10/3).to_i #=> 3
        

        【讨论】:

        • 似乎 OP 意味着 ten 到 10,nine 到 9 等等 :)
        • @slivu OP 提出了多个问题。我只回答最后一个。
        【解决方案4】:

        这是一个简单的字符串查找到它们的等效数字:

        str_to_int_hash = {
          'zero'  => 0,
          'one'   => 1,
          'two'   => 2,
          'three' => 3,
          'four'  => 4,
          'five'  => 5,
          'six'   => 6,
          'seven' => 7,
          'eight' => 8,
          'nine'  => 9,
          'ten'   => 10
        }
        
        str_to_int_hash['ten']
        => 10
        

        很明显还有很多其他的缺失条目,但它说明了这个想法。

        如果你想从一个数字转到字符串,这是起点:

        int_to_str_hash = Hash[str_to_int_hash.map{ |k,v| [v,k] }]
        int_to_str_hash[10]
        => "ten"
        

        【讨论】:

        • 虽然这是小值的起点,但它并没有暗示将其扩展到任意大数所必需的一般性。
        • 这就是我说“简单查找”的原因。由于 OP 没有提到任何超出简单数字的内容,因此在一个非常令人困惑且问得不好的问题中,这就足够了。除此之外,它还需要一段更完整的代码。
        猜你喜欢
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 2016-01-07
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        相关资源
        最近更新 更多