javascript - 如何在 JS 数字中添加千位分隔符以进行字符串输出?
问题描述
我有一个 API 调用正在进行,它将venue_capacity
足球队的返回作为没有千位分隔符的数字。我想将其打印为字符串并包含千位分隔符。
我的想法是在我从 API 获得数据后运行该函数以相应地操作变量venue_capacity
。
这是我目前的尝试(来自这个线程:如何在 JavaScript 中用逗号打印一个数字作为千位分隔符),它返回没有分隔符的数字:
$.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);
}
解决方案
您不必function
在范围内声明 s $.ajax
- 只需一次性执行翻译:
venue_capacity = team_info.api.teams[0].venue_capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
推荐阅读
- laravel-5 - Laravel - 安装新库 - 删除/降级供应商包
- ios - 定义一个可以在运行时在视图之间切换的故事板
- javascript - 在本地主机上从 JavaScript 向 c# 发送 https 请求
- javascript - 自动滚动不会滚动到元素,损坏的 JS
- c# - 强制类型变量为特定的基类类型
- php - 条件逻辑和自定义 FB 像素集成到 WooCommerce
- git - 使用 git bash 时出现此错误的原因是什么
- chef-infra - 如何让 Chef 出错或至少报告 .erb 模板中缺少的变量?
- .net - 捕获和更改系统的日期
- javascript - 是否有不支持后视的浏览器的修复/回调