Forum: Ruby Newbie: Looking for help rounding numbers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
302da8d9b2db88c7f08d35c4a59b2a01?d=identicon&s=25 Jason Bornhoft (jmvbxx)
on 2006-12-29 01:26
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 ""
35b0b4029fd4387842ec88a8e99d84de?d=identicon&s=25 Jason Mayer (Guest)
on 2006-12-29 03:00
(Received via mailing list)
On 12/28/06, Ja Bo <jbornhoft@gmail.com> wrote:
> puts "Input a temperature in Fahrenheit: "
> --
> Posted via http://www.ruby-forum.com/.
>
> I don't see what's wrong with round.

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

output:
Fahrenheit to Centigrade Conversion
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2006-12-29 03:00
(Received via mailing list)
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
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."
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-12-29 03:56
(Received via mailing list)
Phrogz wrote:
>
> 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."
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2006-12-29 04:16
(Received via mailing list)
William James wrote:
[snip]

Cleaner style yet. Nice.
Cheeky monkey. :)
302da8d9b2db88c7f08d35c4a59b2a01?d=identicon&s=25 Jason Bornhoft (jmvbxx)
on 2006-12-29 05:03
Thank you very much for the help!
This topic is locked and can not be replied to.