{"id":355,"date":"2019-08-08T11:21:11","date_gmt":"2019-08-08T03:21:11","guid":{"rendered":"http:\/\/yusemo.com\/?p=355"},"modified":"2019-08-08T11:21:52","modified_gmt":"2019-08-08T03:21:52","slug":"tolocalestring%e5%a5%87%e6%b7%ab%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/yusemo.com\/?p=355","title":{"rendered":"toLocaleString\u5947\u6deb\u6280\u5de7"},"content":{"rendered":"<p><strong>toLocaleString\u4ecb\u7ecd<\/strong><br \/>\ntoLocaleString\u7528\u4e8e\u5c06\u5bf9\u8c61\u6839\u636e\u8bed\u8a00\u7684\u4e0d\u540c\u8f6c\u6362\u6210\u67d0\u79cd\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u6765\u5224\u65ad\u5177\u4f53\u7684\u8868\u73b0\u5f62\u5f0f\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecdNumber\u548cDate\u7c7b\u578b\u7684toLocaleString\u65b9\u6cd5.<br \/>\n\u53c2\u8003\uff1ahttps:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Number\/toLocaleString<\/p>\n<p>\u8c08\u4e00\u4e2a\u9762\u8bd5\u4e2d\u7ecf\u5e38\u4f1a\u88ab\u95ee\u9053\u7684\u95ee\u9898\uff1a\u5982\u4f55\u5bf9\u6570\u5b57\u8fdb\u884c\u5343\u4f4d\u7b26\u683c\u5f0f\u5316\uff1f\u4e00\u822c\u4eba\u53ef\u80fd\u60f3\u5230\u7684\u505a\u6cd5\u662f\u6362\u6210\u5b57\u7b26\u6570\u7ec4\u5faa\u73af\u624b\u52a8\u63d2\u5165,\u6216\u8005\u4f7f\u7528\u6b63\u5219\u7684\u65b9\u6cd5\u3002\u5176\u5b9e\u77e5\u9053\u4e86toLocaleString\u5b8c\u5168\u53ef\u4ee5\u4e0d\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u4e00\u884c\u4ee3\u7801\u5c31\u80fd\u641e\u5b9a\u3002<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/><\/div><\/td><td><div class=\"text codecolorer\">const num = 1122333444455551<br \/>\nnum.toLocaleString() \/\/1,122,333,444,455,551<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Number.prototype.toLocaleString<br \/>\n\u652f\u6301\u4e24\u4e2a\u53c2\u6570\u3002\u4e00\u4e2a\u662f\u8bed\u8a00\u4ee3\u7801\uff0c\u8868\u793a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u6210\u54ea\u56fd\u8bed\u8a00\uff1b\u53e6\u4e00\u4e2a\u662f\u683c\u5f0f\u5316\u65f6\u7684\u53ef\u9009\u7684\u4e00\u4e9b\u5c5e\u6027\uff0c\u5305\u62eclocaleMatcher\u3001style\u3001currency\u3001currencyDisplay\u3001useGrouping\u3001minimumIntegerDigits\u3001minimumFractionDigits\u3001maximumFractionDigits\u3001minimumSignificantDigits\u3001maximumSignificantDigits\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">const num = 1276482;<br \/>\nnum.toLocaleString('zh', { style: 'decimal' }) \/\/ 1,276,482\uff0c\u7eaf\u6570\u5b57\u683c\u5f0f<br \/>\nnum.toLocaleString('zh', { style: 'percent' }) \/\/ 127,648,200%\uff0c\u767e\u5206\u6570\u683c\u5f0f<br \/>\nnum.toLocaleString('zh', { style: 'currency', currency: 'CNY' }); \/\/ \uffe51,276,482.00\uff0c\u4eba\u6c11\u5e01\u5f62\u5f0f<br \/>\nnum.toLocaleString('zh', { style: 'currency', currency: 'cny', currencyDisplay: 'code' }); \/\/ CNY1,276,482.00\uff0ccurrency\u4e0d\u533a\u5206\u5927\u5c0f\u5199<br \/>\nnum.toLocaleString('zh', { style: 'currency', currency: 'cny', currencyDisplay: 'name' }); \/\/ 1,276,482.00\u4eba\u6c11\u5e01<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>\u4e0a\u9762\u63d0\u5230\u7684\u6bd4\u8f83\u957f\u7684\u5c5e\u6027\u7528\u6765\u63a7\u5236\u6574\u6570\u3001\u5c0f\u6570\u4f4d\u6570\u548c\u6709\u6548\u6570\u5b57\u4f4d\u6570\uff0c\u53ef\u4ee5\u5206\u4e3a\u4e24\u7ec4\u3002\u7b2c\u4e00\u7ec4\u662fminimumIntegerDigits\u3001minimumFractionDigits\u3001maximumFractionDigits\uff0c\u5e94\u7528\u573a\u666f\u662f\u81ea\u52a8\u88650\u3002<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"text codecolorer\">const num = 1413.56;<br \/>\nnum.toLocaleString('zh', { minimumIntegerDigits: 7 }); \/\/0,001,413.56<br \/>\n\/\/ \u5982\u679c\u4e0d\u60f3\u8981\u5206\u9694\u7b26\u53ef\u4ee5\u6307\u5b9auseGrouping\u4e3afalse<br \/>\nnum.toLocaleString('zh', { minimumIntegerDigits: 7, useGrouping: false }); \/\/ 0001413.56<br \/>\nnum.toLocaleString('zh', { minimumFractionDigits: 3, useGrouping: false }); \/\/ 1413.560<br \/>\nnum.toLocaleString('zh', { maximumFractionDigits: 1, useGrouping: false }); \/\/ 1413.6<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>\u53e6\u4e00\u7ec4\u662fminimumSignificantDigits\u548cmaximumSignificantDigits\uff0c\u7528\u6765\u63a7\u5236\u6709\u6548\u6570\u5b57\u4f4d\u6570\uff0c\u53ea\u8981\u8bbe\u7f6e\u4e86\u8fd9\u4e00\u7ec4\u5c5e\u6027\uff0c\u7b2c\u4e00\u7ec4\u7684\u5c5e\u6027\u5c31\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"text codecolorer\">const num = 141.56;<br \/>\nnum.toLocaleString('zh', { minimumSignificantDigits: 8 useGrouping: false }); \/\/ 141.56000<br \/>\nnum.toLocaleString('zh', { maximumSignificantDigits: 3, useGrouping: false }); \/\/ 142<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><strong>Date.prototype.toLocaleString<\/strong><br \/>\nDate\u7c7b\u578b\u7684\u8bed\u8a00\u4ee3\u7801\u4e00\u822c\u7528\u7684\u4e0d\u591a\uff0c\u4fdd\u6301\u9ed8\u8ba4\u6216\u8005\u4f7f\u7528zh\u5373\u53ef\uff0c\u683c\u5f0f\u5316\u65f6\u7684\u5e38\u7528\u7684\u5c5e\u6027\u4e3b\u8981\u6709weekday\u3001year\u3001month\u3001day\u3001hour\u3001minute\u3002<\/p>\n<p>weekday\u8868\u793a\u661f\u671f\u51e0\uff08\u4e2d\u6587\u5f62\u5f0f\u6ca1\u6709\uff09\uff0c\u53ef\u9009\u5c5e\u6027\u6709narrow\u3001short\u3001long\uff0c\u7528\u6765\u63a7\u5236\u7f29\u5199\u7684\u5f62\u5f0f<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/><\/div><\/td><td><div class=\"text codecolorer\">const date = new Date();<br \/>\ndate.toLocaleString('en', { weekday: 'narrow' }); \/\/ M<br \/>\ndate.toLocaleString('en', { weekday: 'short' }); \/\/ Mon<br \/>\ndate.toLocaleString('en', { weekday: 'long' }); \/\/ Monday<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>timeZoneName\u8868\u793a\u65f6\u533a\u7684\u8868\u73b0\u5f62\u5f0f\uff0c\u6709short\u548clong\u4e24\u4e2a\u503c<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"text codecolorer\">const date = new Date();<br \/>\ndate.toLocaleString('zh', { timeZoneName: 'short' }); \/\/ 2018\/4\/23 GMT+8 \u4e0b\u53482:17:37<br \/>\ndate.toLocaleString('zh', { timeZoneName: 'long' }); \/\/ &quot;2018\/4\/23 \u4e2d\u56fd\u6807\u51c6\u65f6\u95f4 \u4e0b\u53482:17:37&quot;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>\u5269\u4e0b\u7684\u5c5e\u6027\u53ef\u4ee5\u53d6\u503c\u4e3anumeric\u548c2-digit\u6765\u63a7\u5236\u4f4d\u6570\u5c55\u793a\u3002\u4e0d\u8fc7\u597d\u50cf\u5bf9\u4e8e\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u4e0d\u8d77\u4f5c\u7528<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"text codecolorer\">const date = new Date(Date.UTC(2012, 1, 2, 3, 1, 1)); \/\/ 2012\/2\/2 11:01:01<br \/>\ndate.toLocaleString('zh', { hour12: false, year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); \/\/ 2012\/2\/2 11:01:01<br \/>\ndate.toLocaleString('zh', { hour12: false, year: '2-digit', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }); \/\/ 12\/02\/02 11:01:01<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>\u517c\u5bb9\u6027<\/p>\n<p>MDN\u5730\u5740\uff1a<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Number\/toLocaleString\">toLocaleString<\/a><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>toLocaleString\u4ecb\u7ecd toLocaleString\u7528\u4e8e\u5c06\u5bf9\u8c61\u6839\u636e\u8bed\u8a00\u7684\u4e0d\u540c\u8f6c\u6362\u6210\u67d0\u79cd\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u5b57 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[37,12,36],"_links":{"self":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/355"}],"collection":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":4,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/359"}],"wp:attachment":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}