Javascript 从twitterapi格式化数据
我有一个块显示twitter句柄的跟随者数量。效果很好。唯一的问题是,有11000多个追随者,而且API没有添加逗号。我如何合并JavaScript来格式化数字,使其看起来更漂亮 以下是我使用的twitter代码:Javascript 从twitterapi格式化数据,javascript,twitter,Javascript,Twitter,我有一个块显示twitter句柄的跟随者数量。效果很好。唯一的问题是,有11000多个追随者,而且API没有添加逗号。我如何合并JavaScript来格式化数字,使其看起来更漂亮 以下是我使用的twitter代码: $(function(){ $.ajax({ url: 'http://api.twitter.com/1/users/show.json', data: {screen_name: 'handle'}, dataType: 'jsonp', success:
$(function(){
$.ajax({
url: 'http://api.twitter.com/1/users/show.json',
data: {screen_name: 'handle'},
dataType: 'jsonp',
success: function(data) {
$('#followers').html(data.followers_count);
}
});
});
感谢neiker提供的解决方案帮助
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
$.ajax({
url: 'http://api.twitter.com/1/users/show.json',
data: {screen_name: 'handle'},
dataType: 'jsonp',
success: function(data) {
$('#followers').html(numberWithCommas(data.followers_count));
}
});
检查这个:是的,几天前我看到了,尝试实现,但我对JavaScript了解不够,无法实现它。。然后,更改为:$('#followers').html(data.followers_count);到这个$('#followers').html(numberWithCommas(data.followers_count));哦,你真棒!这确实是我没有尝试组合函数的一种方法。。。非常感谢!
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
$.ajax({
url: 'http://api.twitter.com/1/users/show.json',
data: {screen_name: 'handle'},
dataType: 'jsonp',
success: function(data) {
$('#followers').html(numberWithCommas(data.followers_count));
}
});