列表

详情


JS3. 分隔符

描述

请补全JavaScript代码,要求返回参数数字的千分位分隔符字符串。

示例1

输入:

_comma(12300)

输出:

'12,300'

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1684ms, 内存消耗: 77772KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function _comma(number) {\n                // 补全代码\n                number=number.toString();\n                var arr=[],sum=parseInt(number.length/3),s=\"\";\n                for(var i=0;i<sum;i++){\n                      number.slice(number.length-3)\n                      number.slice(0,number.length-3)\n                      arr.push(number.slice(number.length-3),\",\")\n                      number=number.slice(0,number.length-3)\n                      if(i==sum-1){\n                            arr.push(number);\n                      }\n                 }\n                if(arr[arr.length-1]==\"\")\n                {\n                    var flag=true;\n                    for(var i=arr.length-2;i>-1;i--){\n                          if(flag==true){\n                              flag=false;continue;  \n                          }\n                          s+=arr[i];\n                    }\n                  }else if(arr[arr.length-1]==\"-\"){\n                        var flag=true;\n                        for(var i=arr.length-2;i>-1;i--){\n                            if(flag==true){\n                            flag=false;\n                            s='-';\n                            continue; \n                            }\n                        s+=arr[i];\n                       }\n                   }else{\n                         for(var i=arr.length-1;i>-1;i--){\n                              s+=arr[i];\n                          }\n                        }\n                return s;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1686ms, 内存消耗: 77796KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function _comma(number) {\n                // 补全代码\n                let arr=Math.abs(number).toString().split('').reverse()\n                let i=3\n                while(i<arr.length){\n                    arr.splice(i,0,',')\n                    i+=4\n                }\n                let str=arr.reverse().join('')\n                return number>=0?str:'-'+str\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1687ms, 内存消耗: 77784KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function _comma(number) {\n                // 补全代码\n                number = String(number)\n                let s = number.replace(/(\\d+)(\\d{3})$/, '$1,$2')\n                const reg = /(\\d+)(\\d{3},)/\n                while(reg.test(s)){\n                    s=s.replace(reg, '$1,$2')\n                }\n                return s\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1687ms, 内存消耗: 77816KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function _comma(number) {\n                // 补全代码\n                const num = String(Math.abs(number)).split('.');\n                const numLeft = num[0].split('').reverse();\n                const numRight = num[1] ? '.' + num[1] : '';\n                const commaNum = [];\n                const unit = number < 0 ? '-' : ''\n                let sign = 0;\n                for(let i = 0; i < numLeft.length; i++){\n                    if(sign === 3) {\n                        commaNum.push(',')\n                        sign=0\n                    }\n                    commaNum.push(numLeft[i])\n                    sign++\n                }\n                const result = unit + commaNum.reverse().join('') + numRight;\n                return result\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1704ms, 内存消耗: 77820KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function _comma(number) {\n                // 补全代码\n                number = String(number)\n                let s = number.replace(/(\\d+)(\\d{3})$/, '$1,$2')\n                const reg = /(\\d+)(\\d{3},)/\n                while(reg.test(s)) {\n                    s = s.replace(reg,'$1,$2')\n                }\n                return s\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题