JavaScript | 如何为变量分配十进制,八进制和十六进制值?

Just like C programming language, we can assign integer value in the different format to the variable.

就像C编程语言一样 ,我们可以将不同格式的整数值分配给变量。

  • Assigning decimal value: It can be assigned simply without using any prefix.

    分配十进制值:可以简单地分配它,而无需使用任何前缀。

  • Assigning octal value: It can be assigned by using 0 (zero) prefix with the octal value.

    分配八进制值:可以使用带有八进制值的0 (零)前缀来分配它。

  • Assigning hexadecimal value: It can be assigned using 0x or 0X prefix with the hexadecimal value.

    分配十六进制值:可以使用带有十六进制值的0x或0X前缀进行分配。

Example:

例:

In the given example, we assign assigning 10 which is in decimal format to the variable a, octal format value 012 which is octal of 10 to the variable b, and hexadecimal format value 0x0A which is a hexadecimal value of 10 to the variable c, and printing all values those will be printed in decimal formats.

在给出的例子中,我们分配分配10,它是十进制格式的变量a,八进制格式值012是八进制的10到变量b,和十六进制格式值的0x0A其是10至变量c十六进制值,并打印所有将以十进制格式打印的值。

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>		
		var a = 10; //decimal value
		var b = 012; //octal value
		var c = 0x0A; //hexadecimal value
		
		//printing values
		document.write("a = " + a + "<br>");
		document.write("b = " + b + "<br>");
		document.write("c = " + c + "<br>");
	</script>
</body>
</html>

Output

输出量

a = 10
b = 10
c = 10


翻译自: https://www.includehelp.com/code-snippets/assign-decimal-octal-and-hexadecimal-values-to-the-variables-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值