I have a string which holds a decimal value in it and I need to convert that string into a floating point variable. So an example of the string I have is "5.45" and I want a floating point equivalent so I can add .1 to it. I have searched around the internet, but I only see how to convert a string to an integer.
The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }
my $x = 0; print qq("$x"); prints "0" .
9.3. The Binding Operator, =~ Matching against $_ is merely the default; the binding operator (=~) tells Perl to match the pattern on the right against the string on the left, instead of matching against $_.
You don't need to convert it at all:
% perl -e 'print "5.45" + 0.1;' 5.55
This is a simple solution:
Example 1
my $var1 = "123abc"; print $var1 + 0;
Result
123
Example 2
my $var2 = "abc123"; print $var2 + 0;
Result
0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With