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)".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
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)
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>
Output result:
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!