0

I've got the following pen: http://codepen.io/anon/pen/LVLzvR

I cant quite figure out how to round the number up so you don't break the punctuation. I need to round the number up as I don't want to see a value like 33,333.,333

 //ADDS PUNCTUATION EVERY THREE CHARACTERS
  $('input.numericpunctuation').keyup(function(event){

      var oNum= $(this).val(); // USE THIS NUMBER FOR CALCULATION

      var num = oNum.replace(/,/gi, "").split("").reverse().join("");

      var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));

      console.log(num2);
      console.log(oNum);

      // the following line has been simplified. Revision history contains original.
      $(this).val(num2);
  });

function RemoveRougeChar(convertString){


    if(convertString.substring(0,1) == ","){

        return convertString.substring(1, convertString.length)            

    }
    return convertString;

}

Example input event: If I input 5555, is expect to see (and do see) 5,555. However if I add 5555.55 I get 5,555,.55. Ideally id like to round the number up removing the decimal.

4

1 回答 1

0

问题不只是小数,输入非数字也会导致格式错误,例如,单击King Kong将导致Kin,g K,ong. 因此,您可能想要的是过滤掉非数字,这可以通过将行更改var oNum= $(this).val();为:

var oNum= $(this).val().match(/\d/g).join('');

函数内部的值match是一个 RegEx 对象——如果你以前从未使用过它,那么恭喜!

于 2015-06-11T15:09:12.303 回答