could soemone help me with the following condition, please?
I’m trying to compare $price and $lsec.
if( (sprintf("%.2f", ($price*100+0.5)/100)*1 != $lsec*1) )
{
print Dumper($price,$lsec)
}
Sometimes the dumper prints same numbers(as strings) and jumps in.
Thought, that multiplying with 1 makes floats from them…
Here dumper output:
$VAR1 = '8.5';
$VAR2 = '8.5';
What am I doing wrong?
Thank you,
Greetings and happy easter.
You are correct to say that multiplying a string by 1 will force it to be evaluated as a number, but the numeric
!=comparator will do the same thing. This is presumably a technique you have acquired from other languages as Perl will generally do the right thing and there is no need to force a cast of either operand.Lets take a look at the values you’re comparing:
output
So Perl is correctly saying that 8.51 is unequal to 8.5.
I suspect that your
is intended to round
$priceto two decimal places, but all it does in fact is to increase$priceby 0.005. I think you meant to writebut you also put the value through
sprintfwhich is another way to do the same thing.Either
or
but both is overkill!