Convert a Number to a String in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
2 min

banner

# Convert a Number to a String in TypeScript

Use the String() constructor to convert a number to a string in TypeScript.

The String() constructor converts the supplied value to a string and returns the result.

index.ts
const num = 100; // ๐Ÿ‘‡๏ธ const str: string const str = String(num); console.log(str); // ๐Ÿ‘‰๏ธ "100" console.log(typeof str); // ๐Ÿ‘‰๏ธ "string"

convert number to string in typescript

The code for this article is available on GitHub

We used the String() constructor to convert a number to a string in TypeScript.

The only parameter the function takes is the value that will be converted to a string.

# Converting a negative number to a string

If you convert a negative number to a string, the sign is preserved.

index.ts
const num = -100; const str = String(num); console.log(str); // ๐Ÿ‘‰๏ธ "-100" console.log(typeof str); // ๐Ÿ‘‰๏ธ "string"

convert negative number to string

# Converting a floating-point number to a string

If you pass a number with a decimal to the String() constructor, the decimal is preserved.

index.ts
const num = 14.5; const str = String(num); console.log(str); // ๐Ÿ‘‰๏ธ "14.5"

convert floating point number to string

The code for this article is available on GitHub

# Convert a Number to a String using the toString() method

You can also use the Number.toString method to convert a number to a string in TypeScript.

index.ts
const num = 100; const str = num.toString(); console.log(str); // ๐Ÿ‘‰๏ธ "100" console.log(typeof str); // ๐Ÿ‘‰๏ธ "string"

convert number to string using tostring

The Number.toString() method returns a string representing the specified number.

However, note that you can't directly call a method on a number.

index.ts
// โ›”๏ธ Error const str = 100.toString();

The example shows how trying to call a built-in method on a number throws an error.

# Wrap the number in parentheses before calling toString()

You can wrap the number in parentheses before calling the toString() built-in method.

index.ts
const str = (100).toString(); console.log(str); // ๐Ÿ‘‰๏ธ "100" console.log(typeof str); // ๐Ÿ‘‰๏ธ "string"

wrap number in parentheses before calling tostring

The code for this article is available on GitHub

When using the Number.toString() method, the sign is also preserved after the conversion.

index.ts
const str = (-100).toString(); console.log(str); // ๐Ÿ‘‰๏ธ "-100" console.log(typeof str); // ๐Ÿ‘‰๏ธ "string"

Which approach you pick is a matter of personal preference. I prefer using the String() constructor as it is more widely used in the codebases I've worked on.

If you need to convert a string to a number, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev