Convert String Numbers( in Word Format) to Integer Ruby

Convert string numbers( in word format) to integer ruby

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

how convert string to int in ruby

The solution for this problem is: with regular expression; number is an array with length 1 so I need to convert number[0] to array, like this: num_integer =number[0].to_i

Strictly convert string to integer (or nil)

Use Integer(string)

It will raise an ArgumentError error if the string cannot convert to an integer.

Integer('5abc') #=> ArgumentError: invalid value for Integer(): "5abc"
Integer('5') #=> 5

You'd still need your number_or_nil method if you want the behavior to be that nil is returned when a string cannot be converted.

def number_or_nil(string)
Integer(string || '')
rescue ArgumentError
nil
end

You should be careful to rescue from a particular exception. A bare rescue (such as "rescue nil") will rescue from any error which inherits from StandardError and may interfere with the execution of your program in ways you don't expect. Integer() will raise an ArgumentError, so specify that.

If you'd rather not deal with exceptions and just prefer a shorter version of your number_or_nil you can take advantage of implicit return values and write it as:

def number_or_nil(string)
num = string.to_i
num if num.to_s == string
end

number_or_nil '5' #=> 5
number_or_nil '5abc' #=> nil

This will work the way you expect.

Ruby How to convert string to integer without .to_i

You can use Kernel::Integer:

Integer("219")
#=> 219
Integer("21cat9")
# ArgumentError: invalid value for Integer(): "21cat9"

Sometimes this method is used as follows:

def convert_to_i(str)
begin
Integer(str)
rescue ArgumentError
nil
end
end

convert_to_i("219")
#=> 219
convert_to_i("21cat9")
#=> nil
convert_to_i("1_234")
#=> 1234
convert_to_i(" 12 ")
#=> 12
convert_to_i("0b11011") # binary representation
#=> 27
convert_to_i("054") # octal representation
#=> 44
convert_to_i("0xC") # hexidecimal representation
#=> 12

Some use an "inline rescue" (though it is less selective, as it rescues all exceptions):

def convert_to_i(str)
Integer(str) rescue nil
end

There are similar Kernel methods to convert a string to a float or rational.

Converting numeric string to numeric in Ruby

You can use BigDecimal#frac to achieve what you want

require 'bigdecimal'

def to_numeric(anything)
num = BigDecimal.new(anything.to_s)
if num.frac == 0
num.to_i
else
num.to_f
end
end

It can handle

#floats
to_numeric(2.3) #=> 2.3

#rationals
to_numeric(0.2E-4) #=> 2.0e-05

#integers
to_numeric(1) #=> 1

#big decimals
to_numeric(BigDecimal.new("2"))

And floats, rationals and integers in form of strings, too

How can I convert a string into an integer in RUBY?

To convert string to integer: my_str.to_i, for example

"foo".to_i       # 0
"132".to_i # 132
"132.4".to_i # 132
"foo132".to_i # 0
"132foo".to_i # 132

To find if a string has an odd or even number of characters: my_str.length.odd? or my_str.length.even?, for example:

"foo".length.odd?    # true
"132".length.odd? # true
"foo".length.even? # false
"132".length.even? # false

SEE ALSO:

to_i



Related Topics



Leave a reply



Submit