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

I've been having a problem with int and I can't see what I'm doing wrong.
sub SBS_B2C{ my ($bin_date) = @_; my $year = 1900 + ($bin_date/365); #amended with ."\n" after merlyn's comments print $year."\n"; #output 2004.41643835616 print int($year); #output 20041 } SBS_B2C(38112);
My understanding of int is that it should return 2004 in this case. I tried to look up int on perldocs but the sites out reach at the moment.
Any ideas or am I missing something obvious?

Replies are listed 'Best First'.
•Re: int not working?
by merlyn (Sage) on May 06, 2004 at 15:35 UTC
    sub SBS_B2C{ my ($bin_date) = @_; my $year = 1900 + ($bin_date/365); print $year; #output 2004.41643835616 print int($year); #output 20041 } SBS_B2C(38112);
    I'm gonna go out on a limb here and suggest that (a) this isn't quite the real code (since I see no newlines being printed), and (b) the real code is printing the return value of this subroutine, which will be a 1 from the print operator succeeding, and that's where the extra "1" is coming from after "2004".

    Let's see if my psychic powers are right on both cases. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Or the real print statements are in the other order, and there is a comma between them, ala:
      perl -we'$foo="xyz"; $bar="pdq"; print $foo, print "$bar\n"'
      Your right not excectly the finished code but I am printing in the sub routine just as a test. There is also a new line after the first print in my code. I will amend acordingly.
      Thanks Merlyn I'm not sure I understand why I'm printing the 1 from the previous print statement but changing the code to
      my $year = int(1900 + ($bin_date/365)); print $year."\n";
      Works fine. Thank you for your help.