我有一个 API 调用,它返回venue_capacity一个足球队的数字,没有千位分隔符。我想将其打印为字符串和千位分隔符。我的想法是在我从 API 获得数据后运行该函数以相应地操作变量venue_capacity。这是我当前的尝试返回没有分隔符的数字: $.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); }
2 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
您不必function
在范围内声明s $.ajax
- 只需一次性执行翻译:
venue_capacity = team_info.api.teams[0].venue_capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
DIEA
TA贡献1820条经验 获得超2个赞
尝试改用这个函数:
function formatWithCommat(x){
return x.toLocaleString('en-GB');
}
您可以在此处找到有关它的更多信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
添加回答
举报
0/150
提交
取消