{"id":167737,"date":"2024-05-08T16:09:06","date_gmt":"2024-05-08T08:09:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167737.html"},"modified":"2024-05-08T16:09:11","modified_gmt":"2024-05-08T08:09:11","slug":"javascript%ef%bc%8ctolocalestring%e8%83%bd%e4%b8%8d%e8%83%bd%e8%bd%ac%e5%8c%96%e7%bd%97%e9%a9%ac%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167737.html","title":{"rendered":"JavaScript\uff0ctoLocaleString\u80fd\u4e0d\u80fd\u8f6c\u5316\u7f57\u9a6c\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27033727\/89d20904-8de6-42ea-93b3-19e9f655f64a.webp\" alt=\"JavaScript\uff0ctoLocaleString\u80fd\u4e0d\u80fd\u8f6c\u5316\u7f57\u9a6c\u6570\u5b57\" \/><\/p>\n<p><p><strong>JavaScript \u7684 <code>toLocaleString<\/code> \u65b9\u6cd5\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002<\/strong> \u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u628a\u4e00\u4e2a\u6570\u5b57\u8f6c\u6362\u4e3a\u8868\u793a\u8be5\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff0c\u91c7\u7528\u7684\u662f\u5f53\u524d\u6216\u6307\u5b9a\u8bed\u8a00\u73af\u5883\u7684\u683c\u5f0f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u683c\u5f0f\u5316\u65f6\u95f4\u3001\u65e5\u671f\u3001\u6570\u5b57\u7b49\uff0c\u4ee5\u9002\u5e94\u7528\u6237\u7684\u5730\u57df\u504f\u597d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c<code>toLocaleString<\/code>\u53ef\u4ee5\u5904\u7406\u4f8b\u5982\u8d27\u5e01\u3001\u767e\u5206\u6bd4\u3001\u5c0f\u6570\u70b9\u7cbe\u5ea6\u7b49\u7684\u672c\u5730\u5316\u95ee\u9898\uff0c\u4f46\u5e76\u672a\u63d0\u4f9b\u539f\u751f\u7684\u7f57\u9a6c\u6570\u5b57\u8f6c\u6362\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u5982\u679c\u76ee\u7684\u662f\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u7f57\u9a6c\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\u7f57\u9a6c\u6570\u5b57\u6709\u81ea\u5df1\u72ec\u7279\u7684\u7cfb\u7edf\uff0c\u901a\u8fc7\u7ec4\u5408\u4e0d\u540c\u7684\u62c9\u4e01\u5b57\u6bcd\u8868\u8fbe\u6570\u503c\u3002\u4f8b\u5982\u3001I\uff081\uff09\u3001V\uff085\uff09\u3001X\uff0810\uff09\u3001L\uff0850\uff09\u3001C\uff08100\uff09\u3001D\uff08500\uff09\u548cM\uff081000\uff09\u3002\u4e0b\u9762\u7684\u5185\u5bb9\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728JavaScript\u4e2d\u5b9e\u73b0\u6570\u5b57\u5230\u7f57\u9a6c\u6570\u5b57\u7684\u8f6c\u6362\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528<code>toLocaleString<\/code>\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u5b57\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u81ea\u5b9a\u4e49\u8f6c\u6362\u7f57\u9a6c\u6570\u5b57\u7684\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u7f57\u9a6c\u6570\u5b57\u7684\u8f6c\u6362\u901a\u5e38\u9700\u8981\u8003\u8651\u6570\u5b57\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5e76\u6839\u636e\u7f57\u9a6c\u8ba1\u6570\u89c4\u5219\u8fdb\u884c\u5339\u914d\u548c\u66ff\u4ee3\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684JavaScript\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u7f57\u9a6c\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u6216\u5bf9\u8c61\uff0c\u5305\u542b\u7f57\u9a6c\u6570\u5b57\u7684\u57fa\u672c\u5339\u914d\u89c4\u5219\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const romanMap = [<\/p>\n<p>  { value: 1000, symbol: &#039;M&#039; },<\/p>\n<p>  { value: 900, symbol: &#039;CM&#039; },<\/p>\n<p>  { value: 500, symbol: &#039;D&#039; },<\/p>\n<p>  { value: 400, symbol: &#039;CD&#039; },<\/p>\n<p>  { value: 100, symbol: &#039;C&#039; },<\/p>\n<p>  { value: 90, symbol: &#039;XC&#039; },<\/p>\n<p>  { value: 50, symbol: &#039;L&#039; },<\/p>\n<p>  { value: 40, symbol: &#039;XL&#039; },<\/p>\n<p>  { value: 10, symbol: &#039;X&#039; },<\/p>\n<p>  { value: 9, symbol: &#039;IX&#039; },<\/p>\n<p>  { value: 5, symbol: &#039;V&#039; },<\/p>\n<p>  { value: 4, symbol: &#039;IV&#039; },<\/p>\n<p>  { value: 1, symbol: &#039;I&#039; }<\/p>\n<p>];<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e4b\u540e\uff0c\u6839\u636e\u8fd9\u4e2a\u6620\u5c04\u8868\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u548c\u9010\u6e10\u51cf\u5c0f\u6570\u503c\u7684\u65b9\u5f0f\uff0c\u62fc\u63a5\u51fa\u76f8\u5e94\u7684\u7f57\u9a6c\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function convertToRoman(num) {<\/p>\n<p>  let roman = &#039;&#039;;<\/p>\n<p>  romanMap.forEach((item) =&gt; {<\/p>\n<p>    while (num &gt;= item.value) {<\/p>\n<p>      roman += item.symbol;<\/p>\n<p>      num -= item.value;<\/p>\n<p>    }<\/p>\n<p>  });<\/p>\n<p>  return roman;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u5bf9<code>romanMap<\/code>\u8fdb\u884c\u904d\u5386\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u5224\u65ad\u5982\u679c\u5f53\u524d\u6570\u503c<code>num<\/code>\u5927\u4e8e\u6216\u7b49\u4e8e<code>item.value<\/code>\uff0c\u5219\u5c06\u5bf9\u5e94\u7684<code>item.symbol<\/code>\u52a0\u5230\u7ed3\u679c\u5b57\u7b26\u4e32<code>roman<\/code>\u4e0a\uff0c\u5e76\u4ece<code>num<\/code>\u4e2d\u51cf\u53bb<code>item.value<\/code>\uff0c\u76f4\u5230<code>num<\/code>\u5c0f\u4e8e\u6240\u6709<code>item.value<\/code>\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>toLocaleString<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>toLocaleString<\/code>\u4e0d\u652f\u6301\u76f4\u63a5\u8f6c\u5316\u4e3a\u7f57\u9a6c\u6570\u5b57\uff0c<strong>\u5b83\u5374\u975e\u5e38\u5f3a\u5927\u548c\u7075\u6d3b<\/strong>\uff0c\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u591a\u79cd\u7c7b\u578b\u7684\u6570\u5b57\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u8d27\u5e01\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><pre><code class=\"language-javascript\">let price = 123456.789;<\/p>\n<p>\/\/ \u5047\u8bbe\u4f60\u60f3\u5728\u7f8e\u56fd\u82f1\u8bed\u8bed\u8a00\u73af\u5883\u4e2d\u683c\u5f0f\u5316\u4ef7\u683c<\/p>\n<p>console.log(price.toLocaleString(&#039;en-US&#039;, { style: &#039;currency&#039;, currency: &#039;USD&#039; }));<\/p>\n<p>\/\/ \u8f93\u51fa: &quot;$123,456.79&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u7279\u5b9a\u8d27\u5e01\u683c\u5f0f<\/strong>\uff0c\u8fd9\u91cc\u662f\u4ee5\u7f8e\u5143\u4e3a\u4f8b\u3002\u901a\u8fc7\u8bbe\u7f6e<code>toLocaleString<\/code>\u65b9\u6cd5\u7684<code>options<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u5305\u62ec\u8d27\u5e01\u7c7b\u578b\u3001\u6570\u503c\u7684\u5c0f\u6570\u70b9\u7cbe\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u65e5\u671f\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6570\u5b57\uff0c<code>toLocaleString<\/code>\u4e5f\u53ef\u7528\u4e8e\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u5316\uff0c\u63d0\u4f9b\u4e86\u672c\u5730\u5316\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let today = new Date();<\/p>\n<p>console.log(today.toLocaleString(&#039;en-US&#039;));<\/p>\n<p>\/\/ \u8f93\u51fa\u53d6\u51b3\u4e8e\u4f60\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u7684\u65e5\u671f\u548c\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u5c06\u65e5\u671f\u5bf9\u8c61\u4f20\u9012\u7ed9<code>toLocaleString<\/code><\/strong>\uff0c\u53ef\u4ee5\u5f97\u5230\u672c\u5730\u5316\u7684\u65e5\u671f\u548c\u65f6\u95f4\u8868\u793a\uff0c\u683c\u5f0f\u548c\u8bed\u8a00\u7531<code>toLocaleString<\/code>\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u51b3\u5b9a\u3002\u8fd9\u4f7f\u5f97\u663e\u793a\u672c\u5730\u5316\u7684\u65e5\u671f\u548c\u65f6\u95f4\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u867d\u7136 <code>toLocaleString<\/code> \u65b9\u6cd5\u65e0\u6cd5\u76f4\u63a5\u8f6c\u6362\u7f57\u9a6c\u6570\u5b57\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\u6765\u8fbe\u5230\u76ee\u7684\u3002\u540c\u65f6\uff0c<code>toLocaleString<\/code>\u672c\u8eab\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u672c\u5730\u5316\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u65e0\u8bba\u662f\u5bf9\u6570\u5b57\u3001\u8d27\u5e01\u8fd8\u662f\u65e5\u671f\u65f6\u95f4\u7684\u5904\u7406\uff0c\u90fd\u80fd\u8f7b\u677e\u5b9e\u73b0\u591a\u6837\u5316\u7684\u683c\u5f0f\u5316\u9700\u6c42\u3002\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u5728Web\u5f00\u53d1\u7b49\u573a\u666f\u4e2d\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u4f7f\u5185\u5bb9\u7684\u5448\u73b0\u66f4\u52a0\u53cb\u597d\u3001\u51c6\u786e\u5730\u7b26\u5408\u7528\u6237\u7684\u5730\u57df\u504f\u597d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<ol>\n<li>\n<p><strong>JavaScript\u4e2d\u7684toLocaleString\u65b9\u6cd5\u80fd\u4e0d\u80fd\u7528\u4e8e\u8f6c\u6362\u7f57\u9a6c\u6570\u5b57\uff1f<\/strong><br \/>\ntoLocaleString\u65b9\u6cd5\u662f\u7528\u4e8e\u683c\u5f0f\u5316\u6570\u5b57\u7684\uff0c\u4f46\u5b83\u5e76\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8f6c\u6362\u7f57\u9a6c\u6570\u5b57\u3002\u7f57\u9a6c\u6570\u5b57\u4e0e\u963f\u62c9\u4f2f\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362\u9700\u8981\u7279\u6b8a\u7684\u7b97\u6cd5\u548c\u903b\u8f91\uff0c\u5e76\u4e0d\u662ftoLocaleString\u65b9\u6cd5\u6240\u8bbe\u8ba1\u7528\u6765\u5904\u7406\u7684\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528JavaScript\u5c06\u7f57\u9a6c\u6570\u5b57\u8f6c\u6362\u4e3a\u963f\u62c9\u4f2f\u6570\u5b57\uff1f<\/strong><br \/>\n\u8981\u5c06\u7f57\u9a6c\u6570\u5b57\u8f6c\u6362\u4e3a\u963f\u62c9\u4f2f\u6570\u5b57\uff0c\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u7f57\u9a6c\u6570\u5b57\u7684\u5404\u4e2a\u5b57\u7b26\uff0c\u5e76\u6839\u636e\u89c4\u5219\u8fdb\u884c\u76f8\u5e94\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u5f53\u540e\u9762\u7684\u5b57\u7b26\u6bd4\u524d\u9762\u7684\u5b57\u7b26\u5927\u6216\u76f8\u7b49\u65f6\uff0c\u5c06\u5176\u52a0\u5230\u7ed3\u679c\u4e2d\uff1b\u5f53\u540e\u9762\u7684\u5b57\u7b26\u6bd4\u524d\u9762\u7684\u5b57\u7b26\u5c0f\u65f6\uff0c\u5219\u5c06\u5176\u51cf\u53bb\u3002\u6700\u540e\uff0c\u5c06\u5f97\u5230\u7684\u7ed3\u679c\u8fd4\u56de\u5373\u53ef\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684JavaScript\u5e93\u53ef\u4ee5\u7528\u4e8e\u7f57\u9a6c\u6570\u5b57\u548c\u963f\u62c9\u4f2f\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u6709\u4e00\u4e9b\u73b0\u6210\u7684JavaScript\u5e93\u53ef\u4ee5\u7528\u4e8e\u7f57\u9a6c\u6570\u5b57\u548c\u963f\u62c9\u4f2f\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528&quot;roman-numerals&quot;\u5e93\u6765\u5728JavaScript\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u8f6c\u6362\u64cd\u4f5c\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u7f57\u9a6c\u6570\u5b57\u8f6c\u6362\u4e3a\u963f\u62c9\u4f2f\u6570\u5b57\uff0c\u4ee5\u53ca\u5c06\u963f\u62c9\u4f2f\u6570\u5b57\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002\u4f7f\u7528\u8fd9\u6837\u7684\u5e93\u53ef\u4ee5\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7684 toLocaleString \u65b9\u6cd5\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002 \u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u628a [&hellip;]","protected":false},"author":3,"featured_media":167749,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167737"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=167737"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167737\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167749"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}