[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

converting text

Analogy Analogy

9/18/2007 2:48:00 PM

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!
--
Posted via http://www.ruby-....

9 Answers

Matthew Rudy Jacobs

9/18/2007 3:03:00 PM

0

John Joyce wrote:
> On Sep 18, 2007, at 9:48 AM, Analogy Analogy 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.
--
Posted via http://www.ruby-....

Analogy Analogy

9/18/2007 3:30:00 PM

0

Matthew Rudy wrote:
> John Joyce wrote:
>> On Sep 18, 2007, at 9:48 AM, Analogy Analogy 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.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/18/2007 3:41:00 PM

0

Analogy Analogy 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
--
NP: My Dying Bride - A Doomed Lover
Jabber: sepp2k@jabber.org
ICQ: 205544826

Analogy Analogy

9/18/2007 5:49:00 PM

0

Sebastian Hungerecker wrote:
> Analogy Analogy 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

--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/18/2007 6:20:00 PM

0

Analogy Analogy 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%"]


--
NP: Count Raven - The Lie of Life
Jabber: sepp2k@jabber.org
ICQ: 205544826

Analogy Analogy

9/18/2007 8:14:00 PM

0

Sebastian Hungerecker wrote:
> Analogy Analogy 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!
--
Posted via http://www.ruby-....

tichy

9/18/2007 8:18:00 PM

0

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"]

Analogy Analogy

9/18/2007 9:18:00 PM

0

Szymon tichy wrote:
> 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.
--
Posted via http://www.ruby-....

tichy

9/18/2007 11:44:00 PM

0

On Sep 18, 11:17 pm, Analogy Analogy <analog...@aol.com> 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://en.wikipedia.org/wiki/Regular_e...

http://ruby-doc.org/core/classes/String.ht...

HTH

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