Sebastian Hungerecker
9/18/2007 3:41:00 PM
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