# How to convert string into number

**URL:** <https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685>\
**Category:** Ruby\
**Created:** [July 6, 2007, 12:36am UTC](https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685 "2007-07-06T00:36:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [July 6, 2007, 12:36am UTC](https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685/1 "2007-07-06T00:36:32Z")

</div>

Hi all,

I read several rows of numbers and split each row into an array. I get  
an array of array. By default Ruby treat each number as a string. I want  
to convert each string into a number. The only way I can think of is to  
loop through the old array of array, change each element in each row  
using #to\_f and put them back into a new array. It is not efficient.  
What other options might be better?

Thanks,

Li

# file format

1 100 33 32  
2 500 33 20

…

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [July 6, 2007, 12:50am UTC](https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685/2 "2007-07-06T00:50:39Z")

</div>

Hi,

i would do it like this:

array = []

yourfile.each\_line{|line| array \<\< line.split(’ ').collect(|item|  
item.to\_f)}

Cheers  
detlef

Am Freitag, den 06.07.2007, 07:36 +0900 schrieb Guest:

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [July 6, 2007, 12:51am UTC](https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685/3 "2007-07-06T00:51:40Z")

</div>

From: “Guest” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Li
> 
> # file format
> 
> 1 100 33 32  
> 2 500 33 20

One way would be to use #map:

irb(main):013:0\> rows = [[“1”, “100”, “33”, “32”], [“2”, “500”, “33”,  
“20”]]  
=\> [[“1”, “100”, “33”, “32”], [“2”, “500”, “33”, “20”]]

irb(main):014:0\> rows.each {|row| row.map! {|c| c.to\_f} }  
=\> [[1.0, 100.0, 33.0, 32.0], [2.0, 500.0, 33.0, 20.0]]

You could also use #to\_i if you wanted integers.

Hope this helps,

Bill

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [July 6, 2007, 1:14am UTC](https://www.ruby-forum.com/t/how-to-convert-string-into-number/108685/4 "2007-07-06T01:14:42Z")

</div>

Hi –

On Fri, 6 Jul 2007, Guest wrote:

> Li
> 
> # file format
> 
> 1 100 33 32  
> 2 500 33 20

You could do:

require ‘scanf’  
File.open(“filename”) do |fh|  
fh.scanf("%f%f%f%f") {|n| n }  
end

The point of the {|n| n} is to force a capture of each set of numbers,  
using the block form of scanf. If you’re not sure how many numbers  
are on each line, you’d have to do a little more work but scanf might  
still be a good option.

David
