Perl | Automatic String to Number Conversion or Casting
Last Updated :
12 Feb, 2019
Perl has a different way to deal with the operators because here operator defines how the operands will behave but in other programming languages, operands define how an operator behaves. Casting refers to the conversion of a particular variable's data type to another data type. For example, if there is a string "1234" and after converting it to int data type the output will be an integer 1234. Conversion of string to integer in Perl has many ways. One is to use
Typecasting, while the other one involves use of '
sprintf' function. Sometimes people do not use the word casting in Perl because the whole thing of conversion is automatic.
Typecasting
Type conversion happens when we assign the value of one data type to another. If the data types are compatible, then Perl does Automatic Type Conversion. If not compatible, then they need to be converted explicitly which is known as Explicit Type conversion. There are two types of typecasting:
sprintf function
This sprintf function returns a scalar value, a formatted text string, which gets typecasted according to the code. The command sprintf is a formatter and doesn't print anything at all.
Perl
# Perl code to demonstrate the use
# of sprintf function
# string type
$string1 = "25";
# using sprintf to convert
# the string to integer
$num1 = sprintf("%d", $string1);
$string2 = "13";
# using sprintf to convert
# the string to integer
$num2 = sprintf("%d", $string2);
# applying arithmetic operators
# on int variables
print "Numbers are $num1 and $num2\n";
$sum = $num1 + $num2;
print"Sum of the numbers = $sum\n";
Output:
Numbers are 25 and 13
Sum of the numbers = 38
Similar Reads
Convert a String to an Integer in JavaScript These are the following ways to convert string to an integer in JavaScript:1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. JavaScriptlet s = "100.45"; console.log(typeof Number(s));Outputnumber 2. Using Unary + Oper
2 min read
How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
String to Number Conversion in Julia Julia is a flexible, dynamic, and high-level programming language that can be used to write any application. Also, many of its features can be used for numerical analysis and computational science. Julia is being widely used in machine learning, visualization, and data science. Julia allows type con
3 min read
SQL Query to Get Only Numbers From a String As we know in an SQL database we can insert any type of data. Sometimes in the productions server, the data gets corrupted by two or more rows being merged and being saved in a column. In that case, we can extract the numeric part from that string and save it again. So in this article, we will learn
2 min read
Convert Strings to Numbers and Numbers to Strings in Python Converting strings to numbers and numbers to strings is a common task in Python. In this article, weâll explore simple ways to convert between strings and numbers .Using int() and str()int() and str() functions in Python are commonly used for converting data types. The int() function changes a strin
3 min read
How to Convert String to Number Given a string representation of a numerical value, convert it into an actual numerical value. In this article, we will provide a detailed overview about different ways to convert string to number in different languages. Table of Content Convert String to Number in CConvert String to Number in C++Co
5 min read