如何将JavaScript Number拆分为单个数字?
要将数字撒成数字并打印其总和,请尝试运行以下代码-
示例
<!DOCTYPE html> <html> <body> <script> var a = 28573; var sum = 0; while(a > 0) { sum += a % 10; a = Math.floor(a / 10); } document.write("Sum of digits = "+sum); </script> </body> </html>
输出结果
Sum of digits = 25