Convert Number to String Using toFixed() Method
The toFixed() method is used to round a number to two decimal places and return the result as a string.
Syntax:
let roundedNumber = number.toFixed(digits);
Example: Below is the practical implementation of the above method.
let num: number = 10.555;
let roundedNum: string = num.toFixed(2);
console.log(roundedNum);
console.log(typeof roundedNum);
Output:
10.55
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