Home > Web Front-end > Front-end Q&A > How to convert decimal to octal in JavaScript

How to convert decimal to octal in JavaScript

WBOY
Release: 2022-03-10 15:29:28
Original
1625 people have browsed it

In JavaScript, you can use the toString() method to convert decimal to octal. This method is used for string representation of numbers. When the parameter is set to "8", you can convert the number to 8. Hexadecimal display, the syntax is "number.toString(8)".

How to convert decimal to octal in JavaScript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to convert decimal to octal in JavaScript

JavaScript toString() method

is used for string representation of numbers.

For example, when radix is ​​2, NumberObject will be converted to a string represented by a binary value.

Syntax

number.toString(radix)
Copy after login

radix Optional. Specifies the base for representing numbers, which is an integer between 2 and 36. If this parameter is omitted, base 10 is used. Note, however, that if the parameter is a value other than 10 , the ECMAScript standard allows implementations to return any value.

2 - Numbers are displayed as binary values ​​

8 - Numbers are displayed as octal values ​​

16 - Numbers are displayed as hexadecimal values ​​

Examples are as follows :

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<p id="demo">单击按钮来显示格式化的数字</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var num = 10;
var x = document.getElementById("demo");
x.innerHTML=num.toString(8);
}
</script>
</body>
</html>
Copy after login

Output result:

How to convert decimal to octal in JavaScript

Related recommendations: javascript learning tutorial

The above is the detailed content of How to convert decimal to octal in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template