使用该toString()
方法将Number转换为字符串,然后该length()
方法将为您提供长度。
您可以尝试运行以下代码以了解如何获取数字的长度-
<!DOCTYPE html> <html> <body> <script> var num1 = 344; var num2 = 149856; document.write("Length of number "+num1+" = "+ num1.toString().length); document.write("<br>Length of number "+num2+" = "+ num2.toString().length); </script> </body> </html>
输出结果
Length of number 344 = 3 Length of number 149856 = 6