toString() 方法能够以十六进制、八进制或二进制输出数

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数字</h1>
  6.  
  7. <p>toString() 方法以基数 2 到 36 输出字符串:</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var myNumber = 32;
  13. document.getElementById("demo").innerHTML =
  14. "32 = " + "<br>" +
  15. " Decimal " + myNumber.toString(10) + "<br>" +
  16. " Hexadecimal " + myNumber.toString(16) + "<br>" +
  17. " Octal " + myNumber.toString(8) + "<br>" +
  18. " Binary " + myNumber.toString(2);
  19. </script>
  20.  
  21. </body>
  22. </html>