Converting string to number in javascript/jQuery

后端 未结 9 1874
攒了一身酷
攒了一身酷 2021-01-31 13:08

Been trying to convert the following to number:

9条回答
  •  一生所求
    2021-01-31 13:55

    Although this is an old post, I thought that a simple function can make the code more readable and keeps with jQuery chaining code-style:

    String.prototype.toNum = function(){
        return parseInt(this, 10);
    }
    

    can be used with jQuery:

    var padding_top = $('#some_div').css('padding-top'); //string: "10px"
    var padding_top = $('#some_div').css('padding-top').toNum(); //number: 10`
    

    or with any String object:

    "123".toNum(); //123 (number)`
    

提交回复
热议问题