converting text

A

Analogy Analogy

Hi,

I have a general question, but I can't locate the answer anywhere else;
The answer is probably simple. I hope someone on here can help me out.

Say I have a list (array) of numbers:

[1.80%, 14.50%, ruby, 3.10%]


How can I convert them to:


[1.8%, 14.5%, ruby, 3.1%]


I hope someone on here can help me out with this. Thanks!
 
M

Matthew Rudy

John said:
[1.80%, 14.50%, ruby, 3.10%]
Those are not numbers.
They will be strings.
If you want numbers you'll need to do some work to first remove the %
characters and then convert them to_f (because they're conceptually
floats) After that Ruby will by default lop off the trailing 0's
But you won't have that % character on them.
If you need to use that format and use it a lot, create a class for
them.

I believe String#to_f lops off any trailing characters.

ie. "1.80%".to_f -> 1.8,
it does on my windows install of 1.8.5 at least.
 
A

Analogy Analogy

Matthew said:
John said:
[1.80%, 14.50%, ruby, 3.10%]
Those are not numbers.
They will be strings.
If you want numbers you'll need to do some work to first remove the %
characters and then convert them to_f (because they're conceptually
floats) After that Ruby will by default lop off the trailing 0's
But you won't have that % character on them.
If you need to use that format and use it a lot, create a class for
them.

I believe String#to_f lops off any trailing characters.

ie. "1.80%".to_f -> 1.8,
it does on my windows install of 1.8.5 at least.


Sorry about that, true they are strings

["1.80%", "14.50%", "ruby", "3.10%"]

I want to truncate the "numbers" to one decimal place and still keep the
percent symbol (i.e. 1.80% >> 1.8%). However, I don't want to change any
letter/words (i.e. ruby) at all. I'm new to the language and not sure if
there is an easy way to accomplish this.
 
S

Sebastian Hungerecker

Analogy said:
Sorry about that, true they are strings

["1.80%", "14.50%", "ruby", "3.10%"]

I want to truncate the "numbers" to one decimal place and still keep the
percent symbol (i.e. 1.80% >> 1.8%). However, I don't want to change any
letter/words (i.e. ruby) at all.

["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end

PS: Before this post I thought you only wanted to get rid of trailing zeros
(i.e. "1.80%" becomes "1.8%", but "1.82%" stays the same), you could have
done that as follows:

["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| match.to_f.to_s}
end


HTH,
Sebastian
 
A

Analogy Analogy

Sebastian said:
Analogy said:
Sorry about that, true they are strings

["1.80%", "14.50%", "ruby", "3.10%"]

I want to truncate the "numbers" to one decimal place and still keep the
percent symbol (i.e. 1.80% >> 1.8%). However, I don't want to change any
letter/words (i.e. ruby) at all.

["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end

PS: Before this post I thought you only wanted to get rid of trailing
zeros
(i.e. "1.80%" becomes "1.8%", but "1.82%" stays the same), you could
have
done that as follows:

["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| match.to_f.to_s}
end


I tried cutting and pasting this in SciTE to see if it works...it
didn't, but thanks for directing me in the right direction.
 
S

Sebastian Hungerecker

Analogy said:
I tried cutting and pasting this in SciTE to see if it works...it
didn't, but thanks for directing me in the right direction.

How does it not work? Both versions work just fine in irb:
["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end
=> ["1.8%", "14.5%", "ruby", "3.1%"]
["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| match.to_f.to_s}
end
=> ["1.8%", "14.5%", "ruby", "3.1%"]
 
A

Analogy Analogy

Sebastian said:
Analogy said:
I tried cutting and pasting this in SciTE to see if it works...it
didn't, but thanks for directing me in the right direction.

How does it not work? Both versions work just fine in irb:
["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end
=> ["1.8%", "14.5%", "ruby", "3.1%"]
["1.80%", "14.50%", "ruby", "3.10%"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| match.to_f.to_s}
end
=> ["1.8%", "14.5%", "ruby", "3.1%"]

My fault, I stand corrected. Thank you sir!
 
S

Szymon tichy

Hi group (my first post here).

Imo it is only moderately close to perfection ;)

["10.001%", "r00t", "hax0r", "#x80C and #x670"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end

==> ["10.0%", "r0.0t", "hax0.0r", "#x80.0C and #x670.0"]
 
A

Analogy Analogy

Szymon said:
Hi group (my first post here).

Imo it is only moderately close to perfection ;)

["10.001%", "r00t", "hax0r", "#x80C and #x670"].map do |string|
string.gsub(/\d+(\.\d+)?/) {|match| "%.1f" % match}
end

==> ["10.0%", "r0.0t", "hax0.0r", "#x80.0C and #x670.0"]

Also, ["391", "2", "0.10%]

becomes ["391.0", "2.0", "0.1%]

Don't want to do change/anything to the first and second items. Is there
a good place online, to learn about this type of stuff? Thanks.
 
S

Szymon 'tichy'

Also, ["391", "2", "0.10%]

becomes ["391.0", "2.0", "0.1%]

Don't want to do change/anything to the first and second items.

test = ["391", "2", "0.10%", "10.001%", "#x80C and #x670"]

test.map { |string| string.gsub(/^([+-]?[0-9]*[.][0-9]*[1-9])(0+)%$/,
'\1%') }

==> ["391", "2", "0.1%", "10.001%", "#x80C and #x670"]

better, but test it throughly,
btw, /\A([+-]?\d*[.]\d*[1-9])(0+)%\Z/ is a bit more proper.

Do you want "4%" to become "4.0%" ?
Is there
a good place online, to learn about this type of stuff? Thanks.

http://en.wikipedia.org/wiki/Regular_expressions

http://ruby-doc.org/core/classes/String.html#M000832

HTH

P.S I'm (total) newbie too... is there 'parse float' method ?
'to_s' imo is too poor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top