Search code examples
ruby-on-railsrubymodel

Rails => concatenate integer and string inside model


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 `+'


Solution

  • 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)