用JavaScript中的示例对toString()方法进行编号

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值