# How do I get the first number in Celsius to print and rounded to a whole number?

How do I get the first number in Celsius to print and rounded to a whole number?

``````#!/usr/bin/perl
print "Content-type: text/html\n\n";

sub convert {
my \$fahr = \$_[0];
my \$cels = (\$fahr - 32) * 5/9;
return \$cels}

\$absolute = -459.69;
print "Temperature Program\n\n";
print "What is the Beginning Fahrenheit Temperature: ";
chomp(\$beginning = <STDIN>);
print "\n";
print "What is the Ending Fahrenheit Temperature: ";
chomp(\$ending = <>);
print "\n";
print "What is the Increment Value: ";
chomp(\$incValue = <>);
print "\n\n";

if (\$ending < \$beginning) {
print STDERR "Beginning Temperature must be lower than than Ending Temperature \$
if (\$beginning < \$absolute or \$ending < \$absolute) {
print STDERR "Temperatures cannot go below absolute zero \n";}

print "\tFahrenheit\tCelsius\n\n";
while (\$beginning <= \$ending) {
print "\t\$beginning\t\t\$Celsius\n";
\$beginning = \$beginning + \$incValue,
\$Celsius = &convert(\$beginning);}
print "\n\n";
``````
###### Who is Participating?

Commented:
Is this what you wanted?

``````#!/usr/bin/perl
print "Content-type: text/html\n\n";

sub convert {
my \$fahr = \$_[0];
my \$cels = (\$fahr - 32) * 5/9;
return \$cels}

\$absolute = -459.69;
print "Temperature Program\n\n";
print "What is the Beginning Fahrenheit Temperature: ";
chomp(\$beginning = <STDIN>);
print "\n";
print "What is the Ending Fahrenheit Temperature: ";
chomp(\$ending = <>);
print "\n";
print "What is the Increment Value: ";
chomp(\$incValue = <>);
print "\n\n";

if (\$ending < \$beginning) {
die  "Beginning Temperature must be lower than than Ending Temperature ";
}
if (\$beginning < \$absolute or \$ending < \$absolute) {
die "Temperatures cannot go below absolute zero \n";
}
print "\tFahrenheit\tCelsius\n\n";
while (\$beginning <= \$ending) {
\$Celsius = &convert(\$beginning);
printf "\t%.0f\t\t%.0f\n", \$beginning, \$Celsius;
\$beginning = \$beginning + \$incValue,
}
``````
0

Commented:
``````#! /usr/bin/perl

use strict;
use warnings;

sub toCelcius
{
return (\$_[0] - 32)*5/9;
}
print "Enter Farenheit value : ";
my \$f = <STDIN>;
my \$c = toCelcius(\$f);
print "Celcius is : \$c \n";
printf  "Rounded off %.0f", \$c ;
``````
0

Commented:

``````#!/usr/bin/perl
print "Content-type: text/html\n\n";

sub convert {
my \$fahr = \$_[0];
my \$cels = (\$fahr - 32) * 5/9;
return \$cels}

\$absolute = -459.69;
print "Temperature Program\n\n";
print "What is the Beginning Fahrenheit Temperature: ";
chomp(\$beginning = <STDIN>);
print "\n";
print "What is the Ending Fahrenheit Temperature: ";
chomp(\$ending = <>);
print "\n";
print "What is the Increment Value: ";
chomp(\$incValue = <>);
print "\n\n";

if (\$ending < \$beginning) {
die  "Beginning Temperature must be lower than than Ending Temperature ";
}
if (\$beginning < \$absolute or \$ending < \$absolute) {
die "Temperatures cannot go below absolute zero \n";
}

print "\tFahrenheit\tCelsius\n\n";
while (\$beginning <= \$ending) {
print "\t\$beginning\t\t\$Celsius\n";
\$beginning = \$beginning + \$incValue,
\$Celsius = &convert(\$beginning);}
print "\n\n";
``````
0

Commented:
As you can see in my code, you can round it off by simply using the printf command

printf "%.0f", \$val;

You can also use Math::Round

If you have that class.
0

Author Commented:
farzanj,

The code you posted (that you said you fixed) is my original code... And it still fails to print the first Celsius value AND I couldn't get your other suggestions to work in the code to round the Celsius numbers, either!

PLEASE let me know what I'm doing wrong!  THANK YOU!!!
0

Author Commented:
OK... I changed my code to this:
``````print "\tFahrenheit\tCelsius\n\n";
while (\$beginning <= \$ending) {
printf "\t%.0f\t\t%.0f\n", \$beginning, \$Celsius;
\$beginning = \$beginning + \$incValue,
\$Celsius = &convert(\$beginning);}
print "\n\n";
``````
...and it works beautifully!

HOWEVER, the first value in the list prints a "0" ...

Sample Output:

Temperature Program

What is the Beginning Fahrenheit Temperature: 200

What is the Ending Fahrenheit Temperature: 215

What is the Increment Value: 3

Fahrenheit      Celsius

200             0
203             95
206             97
209             98
212             100
215             102

How is this fixed?
0

Commented:
You were using \$Celsius without calculating it in the loop.  I just changed the order.  You need to calculate it before printing it.
0

Commented:
Here's the output

Temperature Program

What is the Beginning Fahrenheit Temperature: 200

What is the Ending Fahrenheit Temperature: 215

What is the Increment Value: 3

Fahrenheit      Celsius

200            93
203            95
206            97
209            98
212            100
215            102
0

Commented:
When I first got you code, it was not compiling.  I had to use die instead of STDERR.  There were a few more syntax errors that I had to get rid of.
0

Author Commented:
You know, I moved that line ALL AROUND and it didn't do ANYTHING... now I move it, and it fixed the problem!  THANK YOU SO MUCH!!!  I bow to your expertise...
0

Author Commented:
My code works PERFECTLY!  Thank you!!!
0

Commented:
I am glad it did.  You are most welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.