我有一個(gè) API 調(diào)用,它返回venue_capacity一個(gè)足球隊(duì)的數(shù)字,沒(méi)有千位分隔符。我想將其打印為字符串和千位分隔符。我的想法是在我從 API 獲得數(shù)據(jù)后運(yùn)行該函數(shù)以相應(yīng)地操作變量venue_capacity。這是我當(dāng)前的嘗試返回沒(méi)有分隔符的數(shù)字: $.ajax({ method: "GET", async: "True", dataType: "json", url: "https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/teams/team/" + team_id, success: function(response) { var team_info = response; console.log(team_info); team = team_info.api.teams[0].name; founded = team_info.api.teams[0].founded; venue_capacity = team_info.api.teams[0].venue_capacity; function numberWithCommas(venue_capacity) { return venue_capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); } $("#selectedClub").html(team); $("#founded").html(founded); $("#venue_capacity").html(venue_capacity); }
如何將千位分隔符添加到 JS 數(shù)字以進(jìn)行字符串輸出?
慕慕森
2021-11-18 09:40:09