JavaScript | 将十进制转换为八进制,反之亦然

Sometimes we need to convert an integer value which is in decimal format to the octal string in JavaScript or need a decimal value from a given octal string.

有时,我们需要将十进制格式的整数值转换为JavaScript中的八进制字符串,或者需要给定八进制字符串中的十进制值

Read more: How to assign decimal, octal and hexadecimal values to the variables in JavaScript?

阅读更多: 如何为JavaScript中的变量分配十进制,八进制和十六进制值?

In such cases, we can convert a decimal value to the octal string by using the number.toString(8) and octal string to the decimal by using oct_string.parseInt(8).

在这种情况下,我们可以通过使用number.toString(8)和八进制串的十进制通过使用oct_string.parseInt(8) 十进制值转换为八进制串

In both cases, 8 is the base to the number system that says that target or source value format is octal.

在这两种情况下, 8都是表示目标或源值格式为八进制的数字系统的基础。

Example:

例:

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

<body>
	<script>		
		var a = 10;
		var b = 12345;
		
		document.write("a = " + a.toString(8) + "<br>");
		document.write("b = " + b.toString(8) + "<br>");
		
		//conver octal string to the int again
		var oct = "012"; //octal of 10
		var dec = parseInt(oct,8);
		document.write("dec = " + dec + "<br>");
	</script>
</body>
</html>

Output

输出量

a = 12
b = 30071
dec = 10


翻译自: https://www.includehelp.com/code-snippets/convert-decimal-to-octal-and-vice-versa-in-javascript.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值