Updated April 6, 2023
Introduction to TypeScript string to number
A string in TypeScript can be converted to a number using an operator called unary plus (+) operator or using the functions called parseInt function or parseFloat function, or Number function where the unary plus operator converts the numbers represented as a string, Boolean values represented by either true or false and null to numbers. Parseint function converts the given string into an integer, and parseFloat function converts the given string to a number. Number function is similar to the unary plus operator, and it converts the given string to a number, and all these functions return NaN if the given string cannot be converted to a number.
Syntax to declare unary plus(+) operator in TypeScript:
+"string_to_be_converted_to_number";
Where string_to_be_converted_to_number is the string that is to be converted into a number.
Syntax to declare parseInt function in TypeScript:
parseInt(string_to_be_converted_to_number, radix);
Where string_to_be_converted_to_number is the string that is to be converted into a number, and the value of radix is 2 for binary numbers, 8 for octal numbers, 10 for decimal numbers and 16 for hexadecimal numbers. The default value for radix is 16.
Syntax to declare parseFloat function in TypeScript:
parseFloat("string_to_be_converted_to_number");
Where string_to_be_converted_to_number is the string that is to be converted into a number.
Syntax to declare Number function in TypeScript:
Number("string_to_be_converted_to_number");
Where string_to_be_converted_to_number is the string that is to be converted into a number.
Steps to Convert string to number in TypeScript
- A string in TypeScript can be converted to a number using an operator called unary plus (+) operator or using the functions called parseInt function or parseFloat function or Number function.
- The unary plus operator converts the numbers represented as a string, boolean values represented by either true or false and null to numbers.
- The unary plus operator converts an empty string or null to number 0.
- The unary plus operator converts the boolean value true to 1 and the Boolean value false to 0.
- The unary plus operator converts the octal numbers or hexadecimal numbers to a decimal value.
- The unary plus operator also works on scientific notation numbers.
- The unary plus operator returns NaN or Not a Number in case it cannot convert the given string to a number.
- The parseInt function converts the given string into an integer.
- The parseInt function converts infinity, null, boolean values true and false, empty strings to NaN.
- The parseInt function treats the strings beginning with 0x as a hexadecimal number and do not recognize the octal number that begins with 0o.
- The parseFloat function converts the given string into an integer or a decimal number.
- The parseFloat function converts null, boolean values true and false, empty strings to NaN.
- The parseFloat function returns infinity if the given string is infinity.
- The parseFloat function returns 0 for the strings beginning with 0x and 0o and do not recognize hexadecimal or octal numbers.
- The number function converts the given string to a number.
- The number function converts the null or empty string to 0.
- The number function converts the boolean value true to 1 and the boolean value false to 0.
- The number function converts the hexadecimal numbers or octal numbers to decimal values.
- The number function also works on scientific notation numbers.
- The number function returns NaN or Not a Number in case it cannot convert the given string to a number.
Examples of TypeScript string to number
Given below are the examples of TypeScript string to number:
Example #1
TypeScript program to check if a given string is Not a Number or no using isNan() function and then convert the string to a number using the unary plus operator.
Code:
convert("0o100")
//defining a function to convert the given string to a number using unary plus operator
function convert(value)
{
//using isNaN function to check if the given string is a number or not
if (isNaN(+value))
{
console.log("The given value is Not a Number(NaN)")
}
else
{
console.log("The value after converting the given string to a number is:\n")
console.log(value)
}
}
Output:
In the above program, we are defining a function to convert the given string into a number using unary plus operator within which we are also checking if the given string is a number or no, in case it is a number represented as a string, it is converted to a number.
Example #2
TypeScript program to check if a given string is Not a Number or no using isNan() function and then convert the string to a number using parseInt function.
Code:
convert("0o100")
//defining a function to convert the given string to a number using unary plus operator
function convert(value: any)
{
//using isNaN function to check if the given string is a number or not
if (isNaN(+value))
{
console.log("The given value is Not a Number(NaN)")
}
else
{
console.log("The value after converting the given string to a number is:")
console.log(parseInt(value))
}
}
Output:
In the above program, we are defining a function to convert the given string into a number using parseInt function, within which we are also checking if the given string is a number or no, in case it is a number represented as a string, it is converted to a number.
Rules and Regulations
Given below are the rules and regulations for converting string to number in TypeScript:
- The unary plus operator converts the numbers represented as a string, boolean values represented by either true or false and null to numbers.
- The unary plus operator converts an empty string or null to number 0.
- The unary plus operator converts the boolean value true to 1 and the boolean value false to 0.
- The unary plus operator converts the octal numbers or hexadecimal numbers to a decimal value.
- The parseInt function converts infinity, null, boolean values true and false, empty strings to NaN.
- The parseInt function treats the strings beginning with 0x as a hexadecimal number and do not recognize the octal number that begins with 0o.
- The parseFloat function converts null, boolean values true and false, empty strings to NaN.
- The parseFloat function returns infinity if the given string is infinity.
- The parseFloat function returns 0 for the strings beginning with 0x and 0o and do not recognize hexadecimal or octal numbers.
- The number function converts the given string to a number.
- The number function converts the null or empty string to 0.
- The number function converts the boolean value true to 1 and the boolean value false to 0.
- The number function converts the hexadecimal numbers or octal numbers to decimal values.
Recommended Articles
We hope that this EDUCBA information on “TypeScript string to number” was beneficial to you. You can view EDUCBA’s recommended articles for more information.