Number toString()方法 (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.
toString()方法是Number类的一种方法,用于将数字转换为字符串。 它也可以用于将给定数字转换为具有指定数字系统的字符串,例如二进制字符串,八进制字符串和十六进制字符串。
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
翻译自: https://www.includehelp.com/code-snippets/number-toString-method-with-example-in-javascript.aspx