Gossamer Forum
Home : General : Perl Programming :

why does perl round my numbers ?

Quote Reply
why does perl round my numbers ?
-- I have a very basic script:

my ($sec,$min,$hour,$mday,$foo) = localtime time;
$time_now = "$hour$min";

-- I purposely leave out the ":" because i want the whole number.

How come when the time is 10:05, i print $time_now , and it returns '105' ?? It should be 1005. WHERE DID MY EXTRA '0' GO ? It only does this with zero's. If the time was 11:35, it would print 1135 just like i want it to. anyone know how to stop it from doing that ? Thanks in advance for your help :)
Quote Reply
Re: [mtorres] why does perl round my numbers ? In reply to
Hi mtorres,

It's not rounding the numbers. localtime returns the number of minutes without prepending the 0 on miuntes under 10. You'll have to fix it by doing somthing like this:

Code:
$min = '0' . $min if (length $min == 1);

~Charlie
Quote Reply
Re: [Chaz] why does perl round my numbers ? In reply to
ahh. I see what you're talking about.

also, i tried your fix, and it worked perfect. thanks for the help.
Quote Reply
Re: [mtorres] why does perl round my numbers ? In reply to
No worries :)

This fix might be a little faster too:

Code:
$min = '0' . $min if $min < 10;

~Charlie