Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

I need someone to check my code for the extra credit for the "Numbers" section on the "Ruby Foundations" track. HELP!

Am I satisfying the requirements for the extra credit question on the "Numbers" section on the "Ruby Foundations" Track? If not, please help!

Here is the code challenge: "Try writing a Ruby program that asks a user to input numbers and then multiplies them together. Have the program print out the result to two decimal places regardless of whether or not the user entered an integer or floating point number."

Thanks in advance!


puts "Enter your number please." input = gets.chomp puts "Enter your second number please." second = gets.chomp puts input.to_i.round(3) * second.to_i.round(3)

1 Answer

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Hey Erik,

Overall it looks good but it does not round to 2 decimal places wether you test it with floats or integers.

Try this instead:

puts "Enter your number please." 
input = gets.chomp.to_f
puts "Enter your second number please." 
second = gets.chomp.to_f
total = input * second
puts sprintf "%.2f", total

Cheers