Reputation: 111
Is it possible to concatenate an integer and a string inside a model? Something like this:
percent = 50
string = (percent + "%")
Trying this I am getting a Type Error:
TypeError (String can't be coerced into Fixnum): app/models/game.rb:124:in `+'
Upvotes: 2
Views: 2023
Reputation: 13477
You can do it by different ways:
string = "#{number}%" # or number.to_s + "%"
=> "50%"
Or by using number_to_percentage
Rails helper:
string = number_to_percentage(number)
Upvotes: 4
Reputation: 980
percent = 50
percentstring = percent.to_s
string = percentstring + "%"
.to_s = to string
Upvotes: 1