{"id":291,"date":"2006-01-25T09:44:49","date_gmt":"2006-01-25T01:44:49","guid":{"rendered":"http:\/\/j178.mtgbb.com\/33"},"modified":"2006-01-25T09:44:49","modified_gmt":"2006-01-25T01:44:49","slug":"%e6%95%b8%e5%ad%97%e6%a0%bc%e5%bc%8f%e5%8c%96-add-commas","status":"publish","type":"post","link":"https:\/\/j178.mtgbb.com\/?p=291","title":{"rendered":"\u6578\u5b57\u683c\u5f0f\u5316 Add Commas"},"content":{"rendered":"<p>\u8cc7\u6599\u4f86\u6e90\uff1a<a href=\"http:\/\/www.mredkj.com\/javascript\/nfbasic.html\" target=\"_blank\">mredkj<\/a><\/p>\n<p>Javascript \u672c\u8eab\u6c92\u6709 NumberFormat \u4e4b\u985e\u7684 function \u53ef\u4ee5\u7528\u4f86\u5c07\u6578\u5b57\u683c\u5f0f\u5316<\/p>\n<p>\u9019\u88e1\u6709\u500b\u7c21\u55ae\u7684 function\uff0c\u53ef\u4ee5\u8f15\u6613\u505a\u5230 NumberFormat<\/p>\n<p><!--more--><\/p>\n<pre lang=\"javascript\" line=\"1\">\n<span style=\"color: #004101;\">Add Commas<\/span>\n<span style=\"color: #000699;\">JavaScript<\/span>\nfunction addCommas(nStr)\n{\nnStr += '';\nx = nStr.split('.');\nx1 = x[0];\nx2 = x.length &gt; 1 ? '.' + x[1] : '';\nvar rgx = \/(d+)(d{3})\/;\nwhile (rgx.test(x1)) {\nx1 = x1.replace(rgx, '$1' + ',' + '$2');\n}\nreturn x1 + x2;\n}\n<\/pre>\n<p>Change a number such as 1000 into a string 1,000. Pass the value as a string, and it will preserve zeros.&nbsp;<\/p>\n<p><span style=\"color: #000699;\">Examples<\/span><\/p>\n<pre lang=\"javascript\" line=\"1\">\naddCommas(1000)\n\/\/ 1,000\n\naddCommas(1231.897243)\n\/\/ 1,231.897243\n\naddCommas('9999999.00')\n\/\/ 9,999,999.00\n\naddCommas(-500000.99)\n\/\/ -500,000.99\n<\/pre>\n<p><span style=\"color: #004101;\">Other separators<\/span><\/p>\n<pre lang=\"javascript\" line=\"1\">\n<span style=\"color: #000699;\">JavaScript<\/span>\nfunction addSeparatorsNF(nStr, inD, outD, sep)\n{\nnStr += '';\nvar dpos = nStr.indexOf(inD);\nvar nStrEnd = '';\nif (dpos != -1) {\nnStrEnd = outD + nStr.substring(dpos + 1, nStr.length);\nnStr = nStr.substring(0, dpos);\n}\nvar rgx = \/(d+)(d{3})\/;\nwhile (rgx.test(nStr)) {\nnStr = nStr.replace(rgx, '$1' + sep + '$2');\n}\nreturn nStr + nStrEnd;\n}\n<\/pre>\n<p>addSeparatorsNF is part my comprehensive NumberFormat script, but if you only need separator formatting, then you can use the function by itself. It does not require the NumberFormat script.<\/p>\n<p>Not every formatting style is the same. e.g. 1000 may be formatted as 1,000 or 1.000<\/p>\n<p>So addSeparatorsNF gives you the ability to specify the input decimal character, the output decimal character, and the output separator character.<br \/>\nArguments<\/p>\n<p>To use addSeparatorsNF, you need to pass it the following arguments:<\/p>\n<p>nStr: The number to be formatted, as a string or number. No validation is done, so don&#8217;t input a formatted number. If inD is something other than a period, then nStr must be passed in as a string.<\/p>\n<p>inD: The decimal character for the input, such as &#8216;.&#8217; for the number 100.2<\/p>\n<p>outD: The decimal character for the output, such as &#8216;,&#8217; for the number 100,2<\/p>\n<p>sep: The separator character for the output, such as &#8216;,&#8217; for the number 1,000.2<\/p>\n<p><span style=\"color: #000699;\">Examples<\/span><\/p>\n<pre lang=\"javascript\" line=\"1\">\naddSeparatorsNF(43211234.56, '.', '.', ',')\n\/\/ 43,211,234.56\n\naddSeparatorsNF('52093423.003', '.', ',', '.')\n\/\/ 52.093.423,003\n\naddSeparatorsNF('93432,8', ',', '.', ',')\n\/\/ 93,432.8\n\naddSeparatorsNF('584,567890', ',', '.', ',')\n\/\/ 584.567890\n\naddSeparatorsNF(-1.23e8, '.', '.', ',')\n\/\/ -123,000,000\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8cc7\u6599\u4f86\u6e90\uff1amredkj Javascript \u672c\u8eab\u6c92\u6709 NumberFormat \u4e4b\u985e\u7684 function \u53ef &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[26],"class_list":["post-291","post","type-post","status-publish","format-standard","hentry","category-javascript-vbscript","tag-javascript-2"],"_links":{"self":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291"}],"version-history":[{"count":0,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/291\/revisions"}],"wp:attachment":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}