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

      • <bdo id='RyDEr'></bdo><ul id='RyDEr'></ul>

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

        <tfoot id='RyDEr'></tfoot>

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

        将数字转换为十万/千万系统中的单词

        时间:2023-10-12

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

        1. <tfoot id='EGtFF'></tfoot>

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

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

                  <tbody id='EGtFF'></tbody>
                <legend id='EGtFF'><style id='EGtFF'><dir id='EGtFF'><q id='EGtFF'></q></dir></style></legend>
                • 本文介绍了将数字转换为十万/千万系统中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在编写一些将给定数字转换为单词的代码,这是我在谷歌搜索后得到的.但我认为这样一个简单的任务有点太长了.两个正则表达式和两个 for 循环,我想要更简单的.

                  I'm writing some code that converts a given number into words, here's what I have got after googling. But I think it's a bit too long for such a simple task. Two Regular Expressions and two for loops, I want something simpler.

                  我试图用尽可能少的代码行来实现这一点.到目前为止,这是我想出的:

                  I am trying to achieve this in as few lines of code as possible. here's what I've come up with so far:

                  有什么建议吗?

                  var th = ['','thousand','million', 'billion','trillion'];
                  var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
                   var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
                   var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
                   
                  function toWords(s) {
                      s = s.toString();
                      s = s.replace(/[, ]/g,'');
                      if (s != parseFloat(s)) return 'not a number';
                      var x = s.indexOf('.');
                      if (x == -1)
                          x = s.length;
                      if (x > 15)
                          return 'too big';
                      var n = s.split(''); 
                      var str = '';
                      var sk = 0;
                      for (var i=0;   i < x;  i++) {
                          if ((x-i)%3==2) { 
                              if (n[i] == '1') {
                                  str += tn[Number(n[i+1])] + ' ';
                                  i++;
                                  sk=1;
                              } else if (n[i]!=0) {
                                  str += tw[n[i]-2] + ' ';
                                  sk=1;
                              }
                          } else if (n[i]!=0) { // 0235
                              str += dg[n[i]] +' ';
                              if ((x-i)%3==0) str += 'hundred ';
                              sk=1;
                          }
                          if ((x-i)%3==1) {
                              if (sk)
                                  str += th[(x-i-1)/3] + ' ';
                              sk=0;
                          }
                      }
                      
                      if (x != s.length) {
                          var y = s.length;
                          str += 'point ';
                          for (var i=x+1; i<y; i++)
                              str += dg[n[i]] +' ';
                      }
                      return str.replace(/s+/g,' ');
                  }
                  

                  另外,上面的代码转换成英文编号系统,例如 Million/Billion,我需要南亚编号系统,例如 Lakhs 和 Crores.

                  Also, the above code converts to the English numbering system like Million/Billion, I need the South Asian numbering system, like in Lakhs and Crores.

                  推荐答案

                  更新:看起来这比我想象的更有用.我刚刚在 npm 上发布了这个.https://www.npmjs.com/package/num-words

                  Update: Looks like this is more useful than I thought. I've just published this on npm. https://www.npmjs.com/package/num-words

                  这是一个较短的代码.一个正则表达式,没有循环.在南亚编号系统中随心所欲地转换

                  Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system

                  var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
                  var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
                  
                  function inWords (num) {
                      if ((num = num.toString()).length > 9) return 'overflow';
                      n = ('000000000' + num).substr(-9).match(/^(d{2})(d{2})(d{2})(d{1})(d{2})$/);
                      if (!n) return; var str = '';
                      str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
                      str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
                      str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
                      str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
                      str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
                      return str;
                  }
                  
                  document.getElementById('number').onkeyup = function () {
                      document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
                  };

                  <span id="words"></span>
                  <input id="number" type="text" />

                  唯一的限制是,您最多可以转换 9 位数字,我认为这在大多数情况下已经足够了..

                  The only limitation is, you can convert maximum of 9 digits, which I think is more than sufficient in most cases..

                  这篇关于将数字转换为十万/千万系统中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JavaScript 数字转单词 下一篇:为什么 html 输入类型为“数字"?允许在字段中

                  相关文章

                  最新文章

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

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

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

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