Lars
Lars

Reputation: 111

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

Upvotes: 2

Views: 2023

Answers (2)

Ilya
Ilya

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

Boltz0r
Boltz0r

Reputation: 980

percent = 50
percentstring = percent.to_s
string = percentstring + "%"

.to_s = to string

Upvotes: 1

Related Questions