Convert Number to String Using Template Literals
Template literals are a feature in JavaScript (and TypeScript) that allows us to create multi-line strings and embed expressions within them.
Syntax:
let varName: string = `${object}`;
Example: The number variable myFloat is interpolated into the template literal ${myFloat}, which converts it to a string. The resulting string is then assigned to the variable myString.
let myFloat: number = 3.14159;
let myString: string = `${myFloat}`;
console.log(myString);
console.log(typeof myString);
Output:
3.14159
string
How to Convert Number to String in TypeScript ?
Converting floats to strings is a common operation in programming that enables us to work with numeric data in a more flexible and controlled manner.
Below are the approaches:
Table of Content
- Using toString() Method
- Using Template Literals
- Using toFixed() Method
- Using String Constructor
- Using toLocaleString() Method
Contact Us