JavaScript输入中用逗号分隔的数字

时间:2018-07-02 11:19:55

标签: javascript regex decimal nan

在以下过程中,输入数字9999没问题。

但是,当输入为9,999时,结果为NaN。

如何进行编辑?我需要能够使用漫画编号。

我用输入中输入数字的百分比并将其打印到下面的输入中。

final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.procedure_dialog_layout);
dialog.setCanceledOnTouchOutside(true);

procedureListView = (ListView) dialog.findViewById(R.id.procedureList);
procedureListView.setAdapter(new ProcedureAdapter(procedurelist));
procedureListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long 
 id) {


      String selectedurl =procedurelist.get(position).getURL();  

      if(positon==1){  /// for procedure click

         Bundle  bundle = new Bundle();
         bundle.putString("url",selectedurl);
         Intent intent = new 
         Intent(getApplicationContext(),ProcedureActivity.class);
         intent.putExtras(bundle);
         startActivity(intent);
     }else{ //// for other click

         Bundle bundle = new Bundle();
         bundle.putString("url","https://info.orendatech.com/orenda- 
         training");
          Intent intent = new Intent(getApplicationContext(), 
          RequestTrainingActivity.class);
          intent.putExtras(bundle);
          startActivity(intent);
     }

      if(dialog!=null){
          dialog.dismiss();
      }

  }
 });

dialog.show();
function yirmibes() {
  num1 = document.getElementById("firstNumber").value;
  document.getElementById("result").value = (num1 / 100) * 25;
}

function elli() {
  num1 = document.getElementById("firstNumber").value;
  document.getElementById("result").value = (num1 / 100) * 50;
}

function format(input) {
  var nStr = input.value + '';  
  nStr = nStr.replace(/\,/g, "");
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';  
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  input.value = x1 + x2;
}

5 个答案:

答案 0 :(得分:0)

如果要接受逗号值,则需要将输入值解析为float,否则将是文本,并且无法使用text-values进行计算:)

如果数字仅是整数,并且您要去除一千个分隔符,则parseInt应该可以完成此工作,否则您可以替换所有非数字字符,或者最好使用input type="number"仅允许使用数字输入

parseFloat参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

输入类型编号,请参见:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number

答案 1 :(得分:0)

用逗号分隔的数字在Javascript中是String,而不是Number

所以您不能用String来计算Number,这就是为什么您得到NaN的原因。

有很多方法可以实现您想要的,但是重要的是不要使用字符串来计算数字。

在我看来,您可以使用replace()从输入值中删除逗号,然后使用parseInt()获取正确的数字。像这样:

var num1 = document.getElementById("firstNumber").value;
num1 = parseFloat(num1);

答案 2 :(得分:0)

此代码有效,感谢 Alex S。我刚刚在 w3schools 上对其进行了测试。

function yirmibes()
{
  num1 = document.getElementById("firstNumber").value.replace(',', '');
  document.getElementById("result").value = (num1 / 100) * 25;
}
function elli()
{
  num1 = document.getElementById("firstNumber").value.replace(',', '');
  document.getElementById("result").value = (num1 / 100) * 50;
}

function format(input) {
  var nStr = input.value + '';  
  nStr = nStr.replace(/\,/g, "");
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';  
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  input.value = x1 + x2;
}
Number: <input type="text" id="firstNumber" onkeyup="format(this)" />
<br/><br/>
<input type="button" onClick="yirmibes()" Value="25%" />
<input type="button" onClick="elli()" Value="50%" />
<br/><br /> The Result is :
<input type="text" id="result" Value="" />

答案 3 :(得分:0)

您需要使用 .replace() 方法和 .trim() 方法从插入值中删除逗号。

参考: https://stackoverflow.com/a/19245975/7052927

function yirmibes() {
    num1 = document.getElementById("firstNumber").value.replace(/[, ]+/g, "").trim();
    document.getElementById("result").value = (num1 / 100) * 25;
}

function elli() {
    num1 = document.getElementById("firstNumber").value.replace(/[, ]+/g, "").trim();
    document.getElementById("result").value = (num1 / 100) * 50;
}

function format(input) {
    var nStr = input.value + '';
    nStr = nStr.replace(/\,/g, "");
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    input.value = x1 + x2;
}
Number: <input type="text" id="firstNumber" onkeyup="format(this)" />
<br /><br />
<input type="button" onClick="yirmibes()" Value="25%" />
<input type="button" onClick="elli()" Value="50%" />
<br /><br /> The Result is :
<input type="text" id="result" Value="" />

答案 4 :(得分:-1)

在计算结果时删除逗号:

function yirmibes()
{
  num1 = document.getElementById("firstNumber").value.replace(',', '');
  document.getElementById("result").value = (num1 / 100) * 25;
}
function elli()
{
  num1 = document.getElementById("firstNumber").value.replace(',', '');
  document.getElementById("result").value = (num1 / 100) * 50;
}
相关问题