# Newbie: Looking for help rounding numbers

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...

Code:
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 ""

···

--
Posted via http://www.ruby-forum.com/.

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

output:

···

On 12/28/06, Ja Bo <jbornhoft@gmail.com> wrote:

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...

Code:
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 ""

--
Posted via http://www.ruby-forum.com/.

I don't see what's wrong with round.

-----------------------------------
Input a temperature in Fahrenheit:
0
0.0 F is -18 C.

Ja Bo wrote:

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

···

-----------------------------------
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."

Thank you very much for the help!

···

--
Posted via http://www.ruby-forum.com/.

Phrogz wrote:

Ja Bo wrote:
> 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
-----------------------------------
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."

···

-----------------------------------
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."

William James wrote:
[snip]

Cleaner style yet. Nice.
Cheeky monkey. 