How to Convert Number to String in TypeScript ?
Last Updated :
12 Aug, 2024
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:
Using toString() Method
In this approach, we are using toString() method. That is used to convert a value to a string. we will pass the numeric value and it will convert that value into a string.
Syntax:
Object.toString()
Example: In the below code we are passing the number value to the toString() method which converts the float to a string.
JavaScript
let numberValue: number = 7.577474;
let stringValue: string = numberValue.toString();
console.log(stringValue);
console.log(typeof stringValue);
Output:
7.577474
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.
JavaScript
let myFloat: number = 3.14159;
let myString: string = `${myFloat}`;
console.log(myString);
console.log(typeof myString);
Output:
3.14159
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.
JavaScript
let num: number = 10.555;
let roundedNum: string = num.toFixed(2);
console.log(roundedNum);
console.log(typeof roundedNum);
Output:
10.55
string
Using String Constructor
The String constructor can be used to create a new string object. It can also be used to convert a value to a string. in this approach we will see how we can convert float value to string using String Constructor.
Syntax:
let str: string = String(floatValue);
Example: In this example, String(num) converts the number 123.78 into a string "123.78" using the String constructor.
JavaScript
let num: number = 123.78;
let str: string = String(num);
console.log(str);
console.log(typeof str);
Output:
123.78
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.
JavaScript
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
Using toPrecision() Method
The toPrecision() method returns a string representing the number to the specified precision (number of significant digits).
Syntax:
let str: string = num.toPrecision(precision);
Example Code:
JavaScript
let num: number = 5.6789;
let str: string = num.toPrecision(3);
console.log(str);
console.log(typeof str);
Output:
5.68
string
Using JSON.stringify() Method
Another approach to convert floats to strings in TypeScript is by using the JSON.stringify() method. This method is typically used to convert JavaScript values to JSON strings, but it can also be used to convert numbers to strings in a straightforward way.
Example: In the example below, we are passing a float value to the JSON.stringify() method, which converts the float to a string.
JavaScript
let myFloat: number = 45.6789;
let myString: string = JSON.stringify(myFloat);
console.log(myString);
console.log(typeof myString);
Output:
45.6789
string
Using concat() Method
The concat() method is used to concatenate strings. By concatenating an empty string ("") with a number, you can convert the number to a string.
Syntax:
let str: string = numberValue.concat("");
Example: In this example, concatenating an empty string ("") to the number converts it to a string.
JavaScript
let numberValue: number = 789.1011;
let stringValue: string = numberValue + "";
console.log(stringValue);
console.log(typeof stringValue);
Output:
789.1011
string
Similar Reads
How to Convert String to Number in TypeScript? In TypeScript, converting a string to a number is a common operation that can be accomplished using several different methods. Each method offers unique advantages and can be chosen based on the specific requirements of your application. Below are the approaches to convert string to number in TypeSc
4 min read
How to Convert String to JSON in TypeScript ? Converting a string to JSON is essential for working with data received from APIs, storing complex data structures, and serializing objects for transmission. Below are the approaches to converting string to JSON in TypeScript:Table of ContentConvert String to JSON Using JSON.parse()Convert String to
5 min read
How to Convert String to Date in TypeScript ? In TypeScript, conversion from string to date can be done using the Date object and its method. We can use various inbuilt methods of Date object like new Date() constructor, Date.parse(), and Date.UTC.Table of ContentUsing new Date()Using Date.parse() Using Date.UTC()Using new Date()In this approac
2 min read
How to Convert a String to enum in TypeScript? In TypeScript, an enum is a type of class that is mainly used to store the constant variables with numerical and string-type values. In this article, we will learn, how we can convert a string into an enum using TypeScript.These are the two approaches that can be used to solve it:Table of ContentUsi
5 min read
How to Convert an Object to a JSON String in Typescript ? In TypeScript, an object is a collection of related data and functionality. Objects are made up of properties and methods. Properties describe the object, methods describe what it can do.Table of ContentUsing JSON.stringify()Using json-stringify-safe libraryUsing a Custom Serialization FunctionUsing
5 min read