How to conduct arithmetic operations in JQuery?

前端 未结 5 1768
天命终不由人
天命终不由人 2021-01-03 14:04
var price     = $(\'#addprice\').val();
var pass      = $(\'#pass\').val();
var total     = $(\'#totalprice\').attr(\'value\')
var left      = $(\'#leftquota\').attr         


        
5条回答
  •  难免孤独
    2021-01-03 14:27

    use parseFloat, parseInt, or just place a + before string, if you sure it contains a number:

    var text = "123.456";
    var number = +text; // now 'number' is Number, and not String
    

    Also something tells me that this should work faster than parseInt or parseFloat.

    Another way to use Number object:

    var text = "123.456";
    var number = Number(text);
    

    But this is the slowest way.

    More info: http://www.jibbering.com/faq/faq_notes/type_convert.html#tcNumber

提交回复
热议问题