Home » JavaScript

Number toString() Method with Example in JavaScript

JavaScript Number toString() Method: Here, we are going to learn about the toString() Method of Number class with Example.
Submitted by IncludeHelp, on February 20, 2019

Number toString() Method

toString() method is a method of Number class, it is used to convert a number to the string. It can also be used to convert a given number to the string with specified number systems like binary string, octal string, and hexadecimal string.

Syntax:

    Number.toString([radix]);

Here, radix is an optional parameter, it can be used to define the base of the number system in which we have to convert the number as a string.

Examples:

    Input: 10
    num = 75

    Output:
    num.toString() - "75"
    num.toString(2) - "1001011"
    num.toString(8) - "113"
    num..toString(16) - "4B"

Code:

<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
	var num = 75;
	
	var str1 = num.toString();
	var str2 = num.toString(2);
	var str3 = num.toString(8);
	var str4 = num.toString(16);
	
	document.write("str1 = " + str1 + "<br>");
	document.write("str2 = " + str2 + "<br>");
	document.write("str3 = " + str3 + "<br>");
	document.write("str4 = " + str4 + "<br>");
	
</script>
</body>
</html>

Output

str1 = 75
str2 = 1001011
str3 = 113
str4 = 4b




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.