How to ensure javascript addition instead of string concatenation (Not always adding integers)

前端 未结 4 1366
野的像风
野的像风 2020-12-11 19:16

Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation inst

4条回答
  •  长情又很酷
    2020-12-11 19:40

    For some reason.... who knows???

    Using parseFloat works.... when parseInt does not... obviously they are not always the same.

    FWIW.... I'm "adding" data elements from "checked" checkboxes here...

        var AddOns = 0;
    
        $( '.broadcast-channels').each(function(){              
            if ( $(this).prop("checked")) {                             
                var thisAddOn =  parseFloat($(this).data('channel'));      
                AddOns = AddOns + thisAddOn;                                      
            }           
    });
    

提交回复
热议问题