split not working

后端 未结 3 992
余生分开走
余生分开走 2020-12-07 06:29

How do i split decimal numbers? The variable bidnumber is 10.70.

var bidnumber = $(this).parent(\'div\').siblings(\'.advert-details         


        
3条回答
  •  长情又很酷
    2020-12-07 06:42

    • The attribute value is already a string, so you don't have to convert it to a string.
    • The split method doesn't put the array back in the variable.
    • The substr method doesn't put the new string back in the variable.

    So:

    bidnumber = bidnumber.split('.');
    var first = bidnumber[0];
    var second = bidnumber[1];
    second = second.substr(0, 1);
    var finalnumber = first + '.' + second;
    

    Or just:

    bidnumber = bidnumber.split('.');
    bidnumber[1] = bidnumber[1].substr(0, 1);
    var finalnumber = bidnumber.join('.');
    

    Consider also to parse the string to a number and round it:

    var finalnumber = Math.round(parseFloat(bidnumber) * 10) / 10;
    

提交回复
热议问题