Addition turns into concatenation

前端 未结 4 1068
余生分开走
余生分开走 2020-12-12 05:27
var Height=  (rowData.length * 30) + PPPP.top + 10 ;

When i print this i get 9013510... instead of 90 +135+10 = 235. Why does mine turns into conca

相关标签:
4条回答
  • 2020-12-12 06:19

    You can use parseInt for that.

    var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10 ;
    

    I have changed radix to base 10.

    0 讨论(0)
  • 2020-12-12 06:22

    PPPP.top is probably a string. Try:

    var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10 ;
    
    0 讨论(0)
  • 2020-12-12 06:28

    You probably need to convert PPPP.top to a number, eg.

    var Height = (rowData.length * 30) + parseFloat(PPPP.top) + 10;
    
    0 讨论(0)
  • 2020-12-12 06:31

    It's probably treating one of the values incorrectly as a string. Try using parseInt and see if that works:

    var Height=  (rowData.length * 30) + parseInt(PPPP.top, 10) + 10;
    
    0 讨论(0)
提交回复
热议问题