From: Analogy Analogy Date: 2007-09-19T02:49:12+09:00 Subject: Re: converting text 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-forum.com/.