john83reuben has asked for the wisdom of the Perl Monks concerning the following question:

HI I am new to perl programming. I have just started yesterday. I would like to ask a programming question... here is my code:

#!usr/bin/perl
print "Enter temperature in Fahrenheit";
$temp=<STDIN>;
chomp $temp;
$newtemp =((($temp-32)*5)/int(9));
print "Temperature in Celsius is $newtemp\n";

if i enter 75 fahrenheit, the answer is23.8888888888889
. How can i print the amount with no more than 2 decimal
places without using printf and sprintf.

thank you

Replies are listed 'Best First'.
Re: perl newbie question
by bobf (Monsignor) on Jan 06, 2008 at 06:07 UTC

    Welcome to the worlds of Perl and Perlmonks! (Please read Writeup Formatting Tips to learn how to add code tags to your posts.)

    The requirement for disallowing use of printf and sprintf, combined with the nature of the problem itself, make this sound a bit like homework (please forgive me if I am wrong). In general monks don't mind helping with homework as long as you show us you've made a good faith attempt at solving the problem and are very specific about what you need help on. You're part of the way there, so I'll give you a little nudge without explicitly stating one (of the many) ways to do this.

    Think about how people deal with calculating dollars and cents without having to round the cent values. I'm sure a SuperSearch would come up with something.

    Secondly, I don't think your use of int is doing what you expect (currently you're calculating the integer value of 9). Try moving it to a different place in the formula. It may be easier to see what is going on if you break the formula up into separate parts and print the value of each intermediate step.

    Finally, use strict; use warnings; will help tremendously as you learn the syntax of the language. They are your friend.

    Best of luck, and welcome to Perlmonks! Please post a reply if you remain stuck.

    Update: Gah! derby beat me to it.

Re: perl newbie question
by derby (Abbot) on Jan 06, 2008 at 05:57 UTC

    Sure ... but why wouldn't you want to use s/printf?

    At the risk of answering a homework question:

    $newtemp =int(100*((($temp-32)*5)/9))/100;

    -derby
      Note that int always rounds towards zero. To get it to round (more) correctly:
      $newtemp =100*((($temp-32)*5)/9); if ($newtemp > 0) { $newtemp = int($newtemp + 0.5)/100; } else { $newtemp = int($newtemp - 0.5)/100; }
      But (s)printf really is the right solution here...
Re: perl newbie question
by downer (Monk) on Jan 06, 2008 at 15:04 UTC
    this is how i would do it:
    $newt = int((($temp - 32)*5/9)*100); print $newt/100, "\n";
      you might want to consider rounding before you take the int. Because your denominator is pretty large, you shouldn't get any fuzz issues, but you do want to add a half to make sure you get the correct second decimal point.

      int EXPR

      Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should not use this function for rounding: one because it truncates towards 0, and two because machine representations of floating point numbers can sometimes produce counterintuitive results. For example, int(-6.725/0.025) produces -268 rather than the correct -269; that's because it's really more like -268.99999999999994315658 instead. Usually, the sprintf, printf, or the POSIX::floor and POSIX::ceil functions will serve you better than will int().