在JavaScript中缩写千位(1k)和数百万(1m)的本地化数字 [英] Abbreviate a localized number in JavaScript for thousands (1k) and millions (1m)

查看:191
本文介绍了在JavaScript中缩写千位(1k)和数百万(1m)的本地化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下Javascript在我的网站上显示我的Instagram粉丝数量。

I am using the following Javascript to display my Instagram follower count on my site.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    /* 
        Get access token & ID through http://jelled.com/instagram/access-token
        Register your app here @ Instagram http://instagram.com/developer 
        */
    $(function() {
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            cache: true,
            url: "https://api.instagram.com/v1/users/{ID}/?access_token={ACCES_TOKEN}",
            success: function(data) {
                var ig_count = data.data.counts.followed_by.toString();
                ig_count = add_commas(ig_count);
                $(".instagram_count").html(ig_count);
            }
        });

        function add_commas(number) {
            if (number.length > 3) {
                var mod = number.length % 3;
                var output = (mod > 0 ? (number.substring(0, mod)) : '');
                for (i = 0; i < Math.floor(number.length / 3); i++) {
                    if ((mod == 0) && (i == 0)) {
                        output += number.substring(mod + 3 * i, mod + 3 * i + 3);
                    } else {
                        output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
                    }
                }
                return (output);
            } else {
                return number;
            }
        }
    });
</script>
<span class="instagram_count"> </span>

如您所见,有一个功能可以在必要时添加逗号。我还希望在另一个类中显示缩写的关注者计数,例如, 3,291 关注者 3.2k 。因此,将完整的追随者数量保持在一个类别中,而将缩写在另一个类别中。我不是最好的JavaScript,但我慢慢学习。

As you can see, there is a function to add comma's where necessary. I'd like to also display the follower count abbreviated, for example, 3,291 followers as 3.2k, in another class. So keeping the full follower count in one class and the abbreviated in another. I am not the greatest at JavaScript but am slowly learning.

我发现了一个类似的问题(有没有办法将数字变成读者友好的格式?(例如$ 1.1k))但没有运气实现它进入我的JavaScript。

I have found a similar question (Is there a way to round numbers into a reader friendly format? (e.g. $1.1k)) but have had no luck implementing it into my JavaScript.

非常感谢任何帮助。

推荐答案

function intlFormat(num)
{
  return new Intl.NumberFormat().format(Math.round(num*10)/10);
}
function makeFriendly(num)
{
  if(num >= 1000000)
    return intlFormat(num/1000000)+'M';
  if(num >= 1000)
    return intlFormat(num/1000)+'k';
  return intlFormat(num);
}

收益率:

makeFriendly(1234567)
"1.2M"
makeFriendly(123457)
"123.5k"
makeFriendly(1237)
"1.2k"
makeFriendly(127)
"127"

Intl 是用于实现国际化行为的Javascript标准包。 Intl.NumberFormatter 特别是本地化的数字格式化程序。因此,此代码实际上尊重您本地配置的千位和小数分隔符。

Intl is the Javascript standard 'package' for implemented internationalized behaviours. Intl.NumberFormatter is specifically the localized number formatter. So this code actually respects your locally configured thousands and decimal separators.

这篇关于在JavaScript中缩写千位(1k)和数百万(1m)的本地化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆