• <i id='zLmt5'><tr id='zLmt5'><dt id='zLmt5'><q id='zLmt5'><span id='zLmt5'><b id='zLmt5'><form id='zLmt5'><ins id='zLmt5'></ins><ul id='zLmt5'></ul><sub id='zLmt5'></sub></form><legend id='zLmt5'></legend><bdo id='zLmt5'><pre id='zLmt5'><center id='zLmt5'></center></pre></bdo></b><th id='zLmt5'></th></span></q></dt></tr></i><div id='zLmt5'><tfoot id='zLmt5'></tfoot><dl id='zLmt5'><fieldset id='zLmt5'></fieldset></dl></div>

    <small id='zLmt5'></small><noframes id='zLmt5'>

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

        <tfoot id='zLmt5'></tfoot>
      1. <legend id='zLmt5'><style id='zLmt5'><dir id='zLmt5'><q id='zLmt5'></q></dir></style></legend>

        在jQuery中将逗号添加到数字

        时间:2023-05-29

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

                <small id='mAWv3'></small><noframes id='mAWv3'>

                    <tbody id='mAWv3'></tbody>

                  本文介绍了在jQuery中将逗号添加到数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有这些号码

                  109998094456

                  如果需要的话,我要做的就是在正确的位置添加一个逗号,所以它看起来像这样

                  And all i want to do is add a comma in the right place if it needs it so it looks like this

                  10,9998,094456

                  这些都在像这样的 p 标签中 <p class="points">10999</p> 等等.

                  These are all within a p tag like this <p class="points">10999</p> etc.

                  可以吗?

                  我在其他帖子的帮助下在这里尝试过 http://jsfiddle.net/pdWTU/1/ 但似乎无法让它工作

                  I've attempted it here with the help of other posts http://jsfiddle.net/pdWTU/1/ but can't seem to get it to work

                  谢谢

                  杰米

                  更新

                  搞砸了一点,并设法在这里弄清楚 http://jsfiddle.net/W5jwY/1/

                  Messed around a bit and managed to figure it out here http://jsfiddle.net/W5jwY/1/

                  打算看看新的全球化插件,以获得更好的方法

                  Going to look at the new Globalization plugin for a better way of doing it

                  谢谢

                  杰米

                  推荐答案

                  适用于所有浏览器,这就是你所需要的.

                  Works on all browsers, this is all you need.

                    function commaSeparateNumber(val){
                      while (/(d+)(d{3})/.test(val.toString())){
                        val = val.toString().replace(/(d+)(d{3})/, '$1'+','+'$2');
                      }
                      return val;
                    }
                  

                  感谢正则表达式,将其写得简洁明了.这是直接的 JS,但你可以像这样在 jQuery 中使用它:

                  Wrote this to be compact, and to the point, thanks to regex. This is straight JS, but you can use it in your jQuery like so:

                  $('#elementID').html(commaSeparateNumber(1234567890));
                  

                  $('#inputID').val(commaSeparateNumber(1234567890));
                  

                  但是,如果您需要更清洁、更灵活的东西.下面的代码将正确修复小数,删除前导零,并且可以无限使用.感谢评论中的@baacke.

                  However, if you require something cleaner, with flexibility. The below code will fix decimals correctly, remove leading zeros, and can be used limitlessly. Thanks to @baacke in the comments.

                    function commaSeparateNumber(val){
                     val = val.toString().replace(/,/g, ''); //remove existing commas first
                     var valRZ = val.replace(/^0+/, ''); //remove leading zeros, optional
                     var valSplit = valRZ.split('.'); //then separate decimals
                      
                     while (/(d+)(d{3})/.test(valSplit[0].toString())){
                      valSplit[0] = valSplit[0].toString().replace(/(d+)(d{3})/, '$1'+','+'$2');
                     }
                  
                     if(valSplit.length == 2){ //if there were decimals
                      val = valSplit[0] + "." + valSplit[1]; //add decimals back
                     }else{
                      val = valSplit[0]; }
                  
                     return val;
                    }
                  

                  在你的 jQuery 中,像这样使用:

                  And in your jQuery, use like so:

                  $('.your-element').each(function(){
                    $(this).html(commaSeparateNumber($(this).html()));
                  });
                  

                  这里是 jsFiddle.

                  这篇关于在jQuery中将逗号添加到数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如果一千或更多,则将数字格式化为 2.5K,否则为 下一篇:将 24 小时制时间转换为 12 小时制时间,带 AM &

                  相关文章

                  最新文章

                  <legend id='W29qz'><style id='W29qz'><dir id='W29qz'><q id='W29qz'></q></dir></style></legend>
                  <tfoot id='W29qz'></tfoot>

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