2

If you have a string ten, is it possible to convert it to an integer 10 in Ruby? (maybe in rails?)

I value the developers at tryruby.org, and in their tutorial here, it specifically says "to_i converts things to integers (numbers.)" I am wondering why they didn't say "to_i converts STRINGS to integers (numbers.)"

What variable types can be converted from their type to an integer?

chris Frisina
  • 19,086
  • 22
  • 87
  • 167

4 Answers4

11

Check out this gem for handling word to number conversions.

From the readme:

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
Seanny123
  • 8,776
  • 13
  • 68
  • 124
rohit89
  • 5,745
  • 2
  • 25
  • 42
3

How I would have done it.

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

for the purpose of testing;

n_to_s(rand(9999))
Jesson Foo
  • 74
  • 4
1

Since String#to_i picks out only the number characters, it will not work in the way you want. There may be some Rails method related to that, but it surely will not have the method name to_i because its behavior will conflict with the original intent of String#to_i.

It is not only Strings that has to_i. NilClass, Time, Float, Rational (and perhaps some other classes) do as well.

"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
sawa
  • 165,429
  • 45
  • 277
  • 381
-3

This is a simple lookup of strings to their numeric equivalent:

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

It's obvious there are many other missing entries, but it illustrates the idea.

If you want to go from a number to the string, this is the starting point:

int_to_str_hash = Hash[str_to_int_hash.map{ |k,v| [v,k] }]
int_to_str_hash[10]
=> "ten"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    While this is a starting point for small values, it doesn't hint at the generalities that would be necessary to extend it to arbitrarily large numbers. – Michael Nov 27 '12 at 08:44
  • That's why I said "simple lookup". Since the OP doesn't mention anything beyond a simple number, in a very confusing and poorly asked question, this suffices. Beyond this it'll need a much more thorough piece of code. – the Tin Man Nov 27 '12 at 14:17