EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript number to string
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript number to string

TypeScript number to string

Introduction to TypeScript number to string

In typescript, Number to string is a method of conversion which is defined as converting the given number to string using the method provided number data type, which supports floating-point numbers such as decimal or hexadecimal or octal numbers. In general, we can define this method number to string conversion using a number data type method known as toString(), which is used for returning the string format of the given number, which is passed to this method as an argument with a specified base of the given number. Another method is known as tolocaleString(); this also converts a number to a string that takes a number in a locale specified base as an argument and returns the string representing the locale to the equivalent number representation.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are two different methods we are using for converting a number to a string in this article:

  • toString():
obj_num.toString( [int_radix] )

Parameters:

int_radix: This parameter takes number representation which has a base specification with a value between 2 and 36, where this number representation will be in an integer format.

The toString() function returns the string specified format for the given or passed as an argument as number representation with specified base value f the number type.

  • toLocaleString():
obj_num.toLocaleString( [specific_locales [, options]] )

Parameters:

specific_locales: this parameter is used to specify the locale to which the given number needs to be converted, and it will be converted to the equivalent or specified locale.

Options: This parameter is an optional parameter used to specify the particular locale that number belongs to the locale environment.

This method returns a string that is the equivalent value of the number specified, and this value uses the locale of the environment to convert the given number to a string.

How to convert number type to string in Typescript?

In Typescript, there are different data types and different methods provided by these types, therefore for converting any specified number of the particular base to string format, the number data type in Typescript provides functions such as toString() and toLocaleString() methods for the number to string conversion with few examples.

Example:

console.log("Demnstration of number to string conversion using toString() method")
let sample_num: number = 786;
let a = sample_num.toString();
console.log(" The string value of the assigned number is :" +a)
let b = sample_num.toString(2);
console.log(" The string value of the number to base 2 is :" +b)
let c = sample_num.toString(8);
console.log(" The string value of the number to base 8 is :" +c)
let d = sample_num.toString(16);
console.log(" The string value of the number to base 16 is :" +d)
let e = sample_num.toString(36);
console.log(" The string value of the number to base 36 is :" +e)

Output:

TypeScript number to string output 1

In the above program, we can see; first, we have declared a variable of number type “sample_number”, and we have assigned the value as “786” to it and to print the string value of this number with various bases. Firstly, we have called toString() with no parameters to print the same value specified when declared. Then later, we are printing the string value of 786 to base 2, then we are printing the string value to base 8, string value to base 16, and then we are printing the string value of 786 to base 36. In the above screenshot, we can see the output of the number 786 converted to a string value with different bases.

Now we will see another example with the toLocaleString() function in the program below.

Example:

console.log(" Demnstration of converting number to string using toLcaleString() function")
let num : number = 10667.987;
let a = num.toLocaleString();
console.log(" The string value in US English of the given number is :" +a)
let b = num.toLocaleString('de-DE');
console.log(" The string value in German of the given number is :" +b)
let c = num.toLocaleString('ar-EG');
console.log(" The string value in Arabic of the given number is:" +c)

Output:

TypeScript number to string output 2

In the above program, we are trying to convert the given number to a specific locale. In the above code, we are printing in US English, German, Arabic locales where all have the same output whereas some time in German it gives comma from the right side wherein the above code in this version we have got it as “10,667.987” whereas in other older version you will get output for German locale as “10.667,987” so we can observe where the comma is placed after which number. Therefore, converting a number to a string in their particular locale toLocaleString() method is used, and the output of the above code can be seen in the above screenshot.

In Typescript, other than the method toString() and toLocaleString() functions for converting a number to a string, there is some other way also, and they are discussed in the below section.

One way is to append the empty string to the number that needs to be converted to a string which will, in turn, return an object of string type with typeof() method is used for checking the type and at last results in primitive type as a string and to append an empty string to the number typescript provides “+” operator for appending.

Example:

console.log(" Demonstration of converting number to string using append operator")
var a: number = 786
var str1: string = a + '';
console.log(" The given number's type is : " +typeof (a))
console.log(" The given number's type is after converting is : " +typeof (str1))
console.log(" The converted sting of the given number is: " +str1)

Output:

output 3

In the above program, we have declared a variable “a” of number type with “786” as the value assigned to it, and then another variable “str1” of string type where the value stored is the number appended with an empty string such as “a + ‘’ ” using ‘+’ operator. By doing this, when we are printing only the “a’s” type using the “typeof()” method, it will display the type of the variable passed to this method. Therefore when only “a” is passed, it will display a number, and when “str1” is passed, it will display a string. So we can see the output of how the number is converted to string form in the above screenshot.

Conclusion

In this article, we conclude that in typescript to convert number to string; a number data type provides two different methods toString() and toLocaleString(); we have seen how these methods can be demonstrated with examples. Therefore it is very important to also know and learn other primitive types for converting into typescript.

Recommended Articles

We hope that this EDUCBA information on “TypeScript number to string” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Operators
  2. TypeScript Types
  3. TypeScript Versions
  4. What is TypeScript?
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more