Perl weird behaviour in number with decimal point

Viewed 58

So I was writing a perl program to do some calculation and I had put a floating point number as

$x = 00.05;

if I print

print $x * 100.0;

It returns 500.0

But if I do

$x = 0.05; print $x * 100.0;

it prints correctly 5.0;

What is this behaviour? Is there any convention I have to obey that I am missing?

1 Answers

A leading zero means an octal constant, so when you do

my $x = 00.05;

you actually do string concatenation of two octal numbers:

my $x = 00 . 05; # The same as "0" . "5"

which gives you the string "05" and later you do

print $x * 100.0;  # prints 500

since perl interprets as "05" as the number 5

Related