将数字转换为Facebook Like Counter

时间:2013-09-24 17:51:49

标签: c# asp.net

这是我目前的代码:

    public static string FormatNumber(Int64 num)
    {
        if (num >= 100000)
            return FormatNumber(num / 1000) + "Thousand";
        if (num >= 10000)
        {
            return (num / 1000D).ToString("0.#") + "Thousand";
        }
        return num.ToString("#,0");
    }

问题:

我希望将数字转换为facebook,就像反击一样。

示例:

190,000 => “190T”

244,555,232 => “190M 500T”

555,123,456,021 =“555B 123M”

有没有像facebook计数器那样的方法?

2 个答案:

答案 0 :(得分:1)

以下是我对此类问题的解决方案:

object[][] list = {
                              new object[] {"B", 1000000000}, 
                              new object[] {"M", 1000000}, 
                              new object[] {"T", 1000}
                              };

            long num = 123412456255; // Here should be the number of facebook likes
            string returned_string = "";
            foreach (object[] a in list) {
                if (num / Convert.ToInt64(a[1]) >= 1) {
                    returned_string += Convert.ToInt64(num / Convert.ToInt64(a[1])).ToString() + a[0] + " ";
                    num -= Convert.ToInt64(num / Convert.ToInt64(a[1])) * Convert.ToInt64(a[1]);
                }
            } Console.WriteLine(returned_string);

答案 1 :(得分:0)

以下是您想要的一般概念:

        const long BILLION = 1000000000;
        const long MILLION = 1000000;
        const long THOUSAND = 1000;

        long a = 1256766123;
        long b;
        string c = string.Empty;

        if (a >= BILLION)
        {
            b = a / BILLION;
            c += b.ToString() + "B";
            a -= (b * BILLION);
        }

        if (a >= MILLION)
        {
            b = a / MILLION;
            if (c != string.Empty)
                c += " ";
            c += b.ToString() + "M";
            a = a - (b * MILLION);
        }

        if (a >= THOUSAND)
        {
            b = a / THOUSAND;
            if (c != string.Empty)
                c += " ";
            c += b.ToString() + "T";
        }

变量c将包含你的字符串。