Converting text

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!

On Sep 18, 2007, at 9:48 AM, Analogy A. wrote:

[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.

John J. wrote:

On Sep 18, 2007, at 9:48 AM, Analogy A. wrote:

[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.

Matthew R. wrote:

John J. wrote:

On Sep 18, 2007, at 9:48 AM, Analogy A. wrote:

[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.

Sebastian H. wrote:

Analogy A. wrote:

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.

HTH,
Sebastian

Analogy A. wrote:

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%”]

Analogy A. wrote:

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

Sebastian H. wrote:

Analogy A. wrote:

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!

Szymon tichy wrote:

Hi group (my first post here).

Imo it is only moderately close to perfection :wink:

[“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.

Hi group (my first post here).

Imo it is only moderately close to perfection :wink:

[“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”]

On Sep 18, 11:17 pm, Analogy A. [email protected] wrote:

[…]

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://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.