Logical Anomaly
Logical Anomaly

Reputation: 33

How to convert float to percentage in Ruby

I need to turn a floating point number (0.02) into a percentage(2%). What is they syntax in ruby?

Upvotes: 2

Views: 4833

Answers (1)

fmorales
fmorales

Reputation: 149

To convert a float to a percentage, multiply the float by 100. If you need to output it with the % sign, use string interpolation inside a string.

Example:

percentage_value  = 0.02 * 100
puts "#{percentage_value}%"

2.0%

{edited}

Upvotes: 2

Related Questions