Newbie: Looking for help rounding numbers

J

Ja Bo

I can't figure out how to round my answer from this short code that I
wrote. I was trying to use both 'format' and 'round' with no luck...

Thank you all in advance!!!

Code:
puts "Fahrenheit to Centigrade Conversion"
puts "-----------------------------------"

puts "Input a temperature in Fahrenheit: "
STDOUT.flush
ftemp = gets.chomp.to_f

ctemp = ( (ftemp - 32) / 1.8 )
format( "%.2f", ctemp )

puts ""
puts ftemp.to_s + " F is " + ctemp.to_s + " C."
puts ""
 
P

Phrogz

Ja said:
I can't figure out how to round my answer from this short code that I
wrote. I was trying to use both 'format' and 'round' with no luck...

puts <<ENDINTRO
Fahrenheit to Centigrade Conversion
-----------------------------------
Input a temperature in Fahrenheit:
ENDINTRO
STDOUT.flush

ftemp = gets.chomp.to_f
ctemp = ( (ftemp - 32) / 1.8 )

puts "", "#{ftemp} F is #{ctemp.round} C."
puts "#{ftemp} F is %d C." % ctemp
puts "#{ftemp} F is %.2f C." % ctemp

rounded_ctemp = format( "%.2f", ctemp )
puts "#{ftemp} F is #{rounded_ctemp} C."
 
W

William James

Phrogz said:
puts <<ENDINTRO
Fahrenheit to Centigrade Conversion
-----------------------------------
Input a temperature in Fahrenheit:
ENDINTRO
STDOUT.flush

ftemp = gets.chomp.to_f
ctemp = ( (ftemp - 32) / 1.8 )

puts "", "#{ftemp} F is #{ctemp.round} C."
puts "#{ftemp} F is %d C." % ctemp
puts "#{ftemp} F is %.2f C." % ctemp

rounded_ctemp = format( "%.2f", ctemp )
puts "#{ftemp} F is #{rounded_ctemp} C."

puts "Fahrenheit to Centigrade Conversion
-----------------------------------
Input a temperature in Fahrenheit:"

STDOUT.flush

ftemp = gets.to_f
ctemp = (ftemp - 32) / 1.8

rounded_ctemp = format( "%.2f", ctemp )
puts "",
"#{ftemp} F is #{ctemp.round} C.",
"#{ftemp} F is #{ctemp.floor} C.",
"#{ftemp} F is %.2f C." % ctemp,
"#{ftemp} F is #{rounded_ctemp} C."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top