[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie: Looking for help rounding numbers

Jason Bornhoft

12/29/2006 12:27:00 AM

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

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

4 Answers

Gavin Kistner

12/29/2006 2:00:00 AM

0

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

William James

12/29/2006 2:51:00 AM

0

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

Gavin Kistner

12/29/2006 3:12:00 AM

0

William James wrote:
[snip]

Cleaner style yet. Nice.
Cheeky monkey. :)

Jason Bornhoft

12/29/2006 4:03:00 AM

0

Thank you very much for the help!

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