EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript string to number

TypeScript string to number

TypeScript string to number

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

+"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:

TypeScript string to number 1

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:

TypeScript string to number 2

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.

  1. TypeScript typeof
  2. TypeScript let
  3. TypeScript Optional Parameters
  4. TypeScript Cast Object
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more