pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <small id='gDc0k'></small><noframes id='gDc0k'>

      <legend id='gDc0k'><style id='gDc0k'><dir id='gDc0k'><q id='gDc0k'></q></dir></style></legend>

        <bdo id='gDc0k'></bdo><ul id='gDc0k'></ul>

    1. <i id='gDc0k'><tr id='gDc0k'><dt id='gDc0k'><q id='gDc0k'><span id='gDc0k'><b id='gDc0k'><form id='gDc0k'><ins id='gDc0k'></ins><ul id='gDc0k'></ul><sub id='gDc0k'></sub></form><legend id='gDc0k'></legend><bdo id='gDc0k'><pre id='gDc0k'><center id='gDc0k'></center></pre></bdo></b><th id='gDc0k'></th></span></q></dt></tr></i><div class="t9dlzfv" id='gDc0k'><tfoot id='gDc0k'></tfoot><dl id='gDc0k'><fieldset id='gDc0k'></fieldset></dl></div>
      1. <tfoot id='gDc0k'></tfoot>

      2. 如何在 JavaScript 中用逗號打印一個數字作為千位

        How to print a number with commas as thousands separators in JavaScript(如何在 JavaScript 中用逗號打印一個數字作為千位分隔符)

        1. <i id='8JpAG'><tr id='8JpAG'><dt id='8JpAG'><q id='8JpAG'><span id='8JpAG'><b id='8JpAG'><form id='8JpAG'><ins id='8JpAG'></ins><ul id='8JpAG'></ul><sub id='8JpAG'></sub></form><legend id='8JpAG'></legend><bdo id='8JpAG'><pre id='8JpAG'><center id='8JpAG'></center></pre></bdo></b><th id='8JpAG'></th></span></q></dt></tr></i><div class="dhrzbnb" id='8JpAG'><tfoot id='8JpAG'></tfoot><dl id='8JpAG'><fieldset id='8JpAG'></fieldset></dl></div>
          <tfoot id='8JpAG'></tfoot>
              <tbody id='8JpAG'></tbody>
            1. <small id='8JpAG'></small><noframes id='8JpAG'>

                  <bdo id='8JpAG'></bdo><ul id='8JpAG'></ul>

                  <legend id='8JpAG'><style id='8JpAG'><dir id='8JpAG'><q id='8JpAG'></q></dir></style></legend>
                  本文介紹了如何在 JavaScript 中用逗號打印一個數字作為千位分隔符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試在 JavaScript 中打印一個整數,并用逗號作為千位分隔符.例如,我想將數字 1234567 顯示為1,234,567".我該怎么做呢?

                  I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this?

                  這是我的做法:

                  function numberWithCommas(x) {
                      x = x.toString();
                      var pattern = /(-?d+)(d{3})/;
                      while (pattern.test(x))
                          x = x.replace(pattern, "$1,$2");
                      return x;
                  }
                  

                  有沒有更簡單或更優雅的方法來做到這一點?如果它也適用于浮點數會很好,但這不是必需的.在句點和逗號之間決定不需要特定于語言環境.

                  Is there a simpler or more elegant way to do it? It would be nice if it works with floats also, but that is not necessary. It does not need to be locale-specific to decide between periods and commas.

                  推薦答案

                  我使用了 Kerry 的回答中的想法,但簡化了它,因為我只是為我的特定目的尋找簡單的東西.這是我所擁有的:

                  I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I have:

                  function numberWithCommas(x) {
                      return x.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
                  }
                  

                  function numberWithCommas(x) {
                      return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
                  }
                  
                  function test(x, expect) {
                      const result = numberWithCommas(x);
                      const pass = result === expect;
                      console.log(`${pass ? "?" : "ERROR ====>"} ${x} => ${result}`);
                      return pass;
                  }
                  
                  let failures = 0;
                  failures += !test(0,        "0");
                  failures += !test(100,      "100");
                  failures += !test(1000,     "1,000");
                  failures += !test(10000,    "10,000");
                  failures += !test(100000,   "100,000");
                  failures += !test(1000000,  "1,000,000");
                  failures += !test(10000000, "10,000,000");
                  if (failures) {
                      console.log(`${failures} test(s) failed`);
                  } else {
                      console.log("All tests passed");
                  }

                  .as-console-wrapper {
                      max-height: 100% !important;
                  }

                  正則表達式使用 2 個前瞻斷言:

                  The regex uses 2 lookahead assertions:

                  • 一個正數,用于查找字符串中后面連續有 3 個數字的倍數的任何點,
                  • 一個否定斷言,以確保該點僅具有 3 位數字的倍數.替換表達式在此處放置一個逗號.

                  例如,如果你傳遞它 123456789.01,肯定斷言將匹配 7 左邊的每個點(因為 789 是 3 位的倍數,678 是 3 位的倍數,567 等).否定斷言檢查 3 位的倍數后面沒有任何數字.789 后面有一個句點,所以它正好是 3 位數字的倍數,所以用逗號.678 是 3 位數字的倍數,但它后面有一個 9,所以這 3 位數字是一組 4 的一部分,逗號不會去那里.567 也是如此.456789 是 6 位數字,是 3 的倍數,所以前面要加逗號.345678 是 3 的倍數,但它后面有一個 9,所以那里沒有逗號.等等.B 防止正則表達式在字符串開頭放置逗號.

                  For example, if you pass it 123456789.01, the positive assertion will match every spot to the left of the 7 (since 789 is a multiple of 3 digits, 678 is a multiple of 3 digits, 567, etc.). The negative assertion checks that the multiple of 3 digits does not have any digits after it. 789 has a period after it so it is exactly a multiple of 3 digits, so a comma goes there. 678 is a multiple of 3 digits but it has a 9 after it, so those 3 digits are part of a group of 4, and a comma does not go there. Similarly for 567. 456789 is 6 digits, which is a multiple of 3, so a comma goes before that. 345678 is a multiple of 3, but it has a 9 after it, so no comma goes there. And so on. The B keeps the regex from putting a comma at the beginning of the string.

                  @neu-rah 提到如果后面有超過 3 個數字,這個函數會在不需要的地方添加逗號小數點.如果這是一個問題,你可以使用這個功能:

                  @neu-rah mentioned that this function adds commas in undesirable places if there are more than 3 digits after the decimal point. If this is a problem, you can use this function:

                  function numberWithCommas(x) {
                      var parts = x.toString().split(".");
                      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ",");
                      return parts.join(".");
                  }
                  

                  function numberWithCommas(x) {
                      var parts = x.toString().split(".");
                      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ",");
                      return parts.join(".");
                  }
                  
                  function test(x, expect) {
                      const result = numberWithCommas(x);
                      const pass = result === expect;
                      console.log(`${pass ? "?" : "ERROR ====>"} ${x} => ${result}`);
                      return pass;
                  }
                  
                  let failures = 0;
                  failures += !test(0              , "0");
                  failures += !test(0.123456       , "0.123456");
                  failures += !test(100            , "100");
                  failures += !test(100.123456     , "100.123456");
                  failures += !test(1000           , "1,000");
                  failures += !test(1000.123456    , "1,000.123456");
                  failures += !test(10000          , "10,000");
                  failures += !test(10000.123456   , "10,000.123456");
                  failures += !test(100000         , "100,000");
                  failures += !test(100000.123456  , "100,000.123456");
                  failures += !test(1000000        , "1,000,000");
                  failures += !test(1000000.123456 , "1,000,000.123456");
                  failures += !test(10000000       , "10,000,000");
                  failures += !test(10000000.123456, "10,000,000.123456");
                  if (failures) {
                      console.log(`${failures} test(s) failed`);
                  } else {
                      console.log("All tests passed");
                  }

                  .as-console-wrapper {
                      max-height: 100% !important;
                  }

                  @tjcrowder 指出,現在 JavaScript 有lookbehind (支持信息),可以在正則表達式本身解決:

                  @t.j.crowder pointed out that now that JavaScript has lookbehind (support info), it can be solved in the regular expression itself:

                  function numberWithCommas(x) {
                      return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
                  }
                  

                  function numberWithCommas(x) {
                      return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
                  }
                  
                  function test(x, expect) {
                      const result = numberWithCommas(x);
                      const pass = result === expect;
                      console.log(`${pass ? "?" : "ERROR ====>"} ${x} => ${result}`);
                      return pass;
                  }
                  
                  let failures = 0;
                  failures += !test(0,               "0");
                  failures += !test(0.123456,        "0.123456");
                  failures += !test(100,             "100");
                  failures += !test(100.123456,      "100.123456");
                  failures += !test(1000,            "1,000");
                  failures += !test(1000.123456,     "1,000.123456");
                  failures += !test(10000,           "10,000");
                  failures += !test(10000.123456,    "10,000.123456");
                  failures += !test(100000,          "100,000");
                  failures += !test(100000.123456,   "100,000.123456");
                  failures += !test(1000000,         "1,000,000");
                  failures += !test(1000000.123456,  "1,000,000.123456");
                  failures += !test(10000000,        "10,000,000");
                  failures += !test(10000000.123456, "10,000,000.123456");
                  if (failures) {
                      console.log(`${failures} test(s) failed`);
                  } else {
                      console.log("All tests passed");
                  }

                  .as-console-wrapper {
                      max-height: 100% !important;
                  }

                  (?<!.d*) 是一個否定的lookbehind,表示匹配項前面不能有 . 后跟零個或多個數字.負面的lookbehind比splitjoin解決方案更快(比較),至少在 V8 中.

                  (?<!.d*) is a negative lookbehind that says the match can't be preceded by a . followed by zero or more digits. The negative lookbehind is faster than the split and join solution (comparison), at least in V8.

                  這篇關于如何在 JavaScript 中用逗號打印一個數字作為千位分隔符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 傳播運算符上的意外令牌)
                  Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以將標志傳遞給 Gulp 以使其以不同的方式運行任務?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務)
                  Visual Studio 2015 crashes when opening Javascript files(打開 Javascript 文件時 Visual Studio 2015 崩潰)
                  Detect FLASH plugin crashes(檢測 FLASH 插件崩潰)
                  • <small id='M654J'></small><noframes id='M654J'>

                      <bdo id='M654J'></bdo><ul id='M654J'></ul>
                      1. <i id='M654J'><tr id='M654J'><dt id='M654J'><q id='M654J'><span id='M654J'><b id='M654J'><form id='M654J'><ins id='M654J'></ins><ul id='M654J'></ul><sub id='M654J'></sub></form><legend id='M654J'></legend><bdo id='M654J'><pre id='M654J'><center id='M654J'></center></pre></bdo></b><th id='M654J'></th></span></q></dt></tr></i><div class="pjlrtbl" id='M654J'><tfoot id='M654J'></tfoot><dl id='M654J'><fieldset id='M654J'></fieldset></dl></div>
                        <legend id='M654J'><style id='M654J'><dir id='M654J'><q id='M654J'></q></dir></style></legend>

                            <tfoot id='M654J'></tfoot>

                              <tbody id='M654J'></tbody>
                            主站蜘蛛池模板: 心得体会网_心得体会格式范文模板 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 网站优化公司_北京网站优化_抖音短视频代运营_抖音关键词seo优化排名-通则达网络 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 哔咔漫画网页版在线_下载入口访问指引| 电缆接头_防水接头_电缆防水接头 - 乐清市新豪电气有限公司 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 瑞典Blueair空气净化器租赁服务中心-专注新装修办公室除醛去异味服务! | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | 一航网络-软件测评官网 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 带式压滤机_污泥压滤机_污泥脱水机_带式过滤机_带式压滤机厂家-河南恒磊环保设备有限公司 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 模具钢_高速钢_不锈钢-万利钢金属材料 | 永嘉县奥阳陶瓷阀门有限公司 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | IWIS链条代理-ALPS耦合透镜-硅烷预处理剂-上海顶楚电子有限公司 lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 福建自考_福建自学考试网| 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 工控机-工业平板电脑-研华工控机-研越无风扇嵌入式box工控机 |