Convert Number to String Using toLocaleString() Method
The toLocaleString() method converts a number into a string, using a locale-specific representation of the number. This method provides flexibility in formatting numbers based on different locales, such as specifying the language, country, and desired formatting options.
Syntax:
dateObj.toLocaleString(locales, options)
Example: This TypeScript code converts the number 1234567.89 to a string using `toLocaleString(‘en-US’)`, formatting it with US locale settings, and outputs the result, confirming its type as string.
let num: number = 1234567.89;
let str: string = num.toLocaleString('en-US');
console.log(str);
console.log(typeof str);
Output:
1,234,567.89
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