Sometimes, we want to convert a string to number in TypeScript.
In this article, we’ll look at how to convert a string to number in TypeScript.
How to convert a string to number in TypeScript?
To convert a string to number in TypeScript, we can use the +
operator, parseInt
or parseFloat
functions.
For instance, we write:
const x = "32";
const y: number = +x;
to convert x
to a floating point number with +
Or we write
const x = "32";
const y: number = parseInt(x);
to convert x
to an integer with parseInt
const x = "32";
const y: number = parseFloat(x);
to convert x
to a floating point number with parseFloat
Conclusion
To convert a string to number in TypeScript, we can use the +
operator, parseInt
or parseFloat
functions.