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