Simple perl help - converting numbers

Hi friends,

I'm very new to perl and got some requirement.
I've input numbers which has size of 17 characters [including +/- sign and .] like below:

-22500.0000000000
58750.00000000000
4944.000000000000
-900.000000000000
272.0000000000000

I need to convert these numbers from negative to positive and positive to negative. After the conversion again I need to get 17 characters.
After some googling below is the part of the script I've written.

$num = -22500.0000000000
$num = sprintf("%17f" - $num);

But this is not giving me the desired output. My requirement to the above numbers should be as below:

22500.00000000000
-58750.0000000000
-4944.00000000000
900.0000000000000
-272.000000000000

Could any one help me please?

Thanks and Regards,
Mysore Ganapati

Try:

perl -pe 's/^-//&&s/$/0/||s/^/-/&&s/0$//' file
1 Like

Thank you for the answer.
But I need to convert the numbers inside the perl script using constants and not on command prompt.
How can I assign the converted values to the constant '$num' as per my example? please inform.

Thanks.
Mysore Ganapati.:confused:

$num = -22500.0000000000;
if ($num =~ /^-/) {
    $num =~ s/^-//;
    $num =~ s/$/0/;
}
else {
    $num =~ s/^/-/;
    $num =~ s/0$//;
}
print "$num\n";

great help, thank you very much for useful information.:b: