Convert String to Number in Ruby

来源:互联网 时间:1970-01-01

When reading data from a file or from other external resources, they always arrive in Ruby as Stringobjects.

If we would like to use them as numbers we first need to convert them to numbers.

But which number and how?

The Stringobjects in Ruby have several methods to convert the string object into a number.

to_iwill convert the String to an Integer. to_fwill convert the String to an Float, a floating pont to_rwill convert the String to a Rationalnumber. to_cwill convert the String to a Complexnumber. Concatenation

Given two numerical values that are actually Stringobject (because of the quotation marks around them), if we use the +operator it will work as concatenation.

a = "2"b = "3"puts a+b # 23 no implicit conversion of Fixnum into String (TypeError)

If one of the values is a Stringobject and the other one is a real number (no quotes), and we try to add them together as in the next example:

puts "2"+3

We'll get an exception: no implicit conversion of Fixnum into String (TypeError)

String tha looks like an integer

A Stringthat holds an integer can be converted to an Integer, a Float, a Rational number, or a Complex number:

puts a.to_i # 2puts a.to_f # 2.0puts a.to_r # 2/1puts a.to_c # 2+0i Setting the base: Converting binary, octal, hexadecimal to decimal

Normally to_iassumes that our original number in the String object is in 10-base representation, but if you would like to change that?

What if you'd like to treat the String as a binary number, an octal number, or a hexadecimal number? You just pass base=with the appropriate number to the to_imethod:

puts "11".to_i # 11puts "11".to_i(base=2) # 3puts "11".to_i(base=16) # 17

Of course hexadecimal "numbers" can also contain the letters a-f. The to_ifuction can deal with that too.

puts "aB".to_i(base=16) # 171

Which brings up the question, what will happen if we use to_iwithout base on a string with hexadecimal number in it, or just any base with values that are not part of the 'digits' it can handle? They all silently return 0.

puts "aB".to_i # 0puts "9".to_i(base=8) # 0

That leads us to the question: What happens if not all the characters ar convertable to number?: The answer is simple. to_iwill convert all the 'digits' on from the beginning of the string up to the point where it does not understand any more. Then it will abandon the rest of the string. Even if there are more understandable digits later on.

puts "2x3".to_i # 2puts "2 3".to_i # 2 Converting to Floating point and Rational number

We can use the other methods to convert a string to a Floating point, a Rational number, or even a Complex number: Some of thoes will understand a decimal point in the String

c = "14.6"puts c.to_i # 14puts c.to_f # 14.6puts c.to_r # 73/5puts c.to_c # 14.6+0i

Some of them will even understand the letter emarking exponent in the String:

e = "2.3e4x5"puts e # 2.3e4x5puts e.to_i # 2puts e.to_f # 23000.0puts e.to_r # 23000/1puts e.to_c # 23000.0+0i Full example

examples/ruby/string_to_number.rb

a = "2"b = "3"puts a+b # 23puts '-------'# puts "2"+3 # no implicit conversion of Fixnum into String (TypeError)puts a.to_i # 2puts a.to_f # 2.0puts a.to_r # 2/1puts a.to_c # 2+0iputs '-------'puts "11".to_i # 11puts "11".to_i(base=2) # 3puts "11".to_i(base=8) # 9puts "11".to_i(base=16) # 17puts '-------'puts "aB".to_i(base=16) # 171puts "aB".to_i # 0puts "9".to_i(base=8) # 0puts '-------'puts "2x3".to_i # 2puts "2 3".to_i # 2puts '-------'c = "14.6"puts c.to_i # 14puts c.to_f # 14.6puts c.to_r # 73/5puts c.to_c # 14.6+0iputs '-------'e = "2.3e4x5"puts e # 2.3e4x5puts e.to_i # 2puts e.to_f # 23000.0puts e.to_r # 23000/1puts e.to_c # 23000.0+0iputs '-------'


相关阅读:
Top