简体   繁体   English

在 JavaScript 中将本地化数字缩写为千 (1k) 和百万 (1m)

[英]Abbreviate a localized number in JavaScript for thousands (1k) and millions (1m)

I am using the following Javascript to display my Instagram follower count on my site.我正在使用以下 Javascript 在我的网站上显示我的 Instagram 粉丝数量。

<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>

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.我还想在另一个类中显示缩写的关注者计数,例如3,291关注者为3.2k 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.我不是最擅长 JavaScript,但我正在慢慢学习。

I have found a similar question ( Is there a way to round numbers into a reader friendly format? (eg $1.1k) ) but have had no luck implementing it into my JavaScript.我发现了一个类似的问题( 有没有办法将数字四舍五入为读者友好的格式?(例如 $1.1k) )但没有将它实现到我的 JavaScript 中。

Any help is greatly appreciated.任何帮助是极大的赞赏。

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);
}

Yields:产量:

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

Intl is the Javascript standard 'package' for implemented internationalized behaviours. Intl是用于实现国际化行为的 Javascript 标准“包”。 Intl.NumberFormatter is specifically the localized number formatter . Intl.NumberFormatter特别是本地化的数字格式器 So this code actually respects your locally configured thousands and decimal separators.所以这段代码实际上尊重你本地配置的千位和十进制分隔符。

new Intl.NumberFormat('en-GB', { notation: "compact", compactDisplay: "short" }).format(9876)

output 9.9K输出9.9K

Credit : mdn信用: mdn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM