0

这是我当前的代码:

    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"

有没有像脸书柜台这样的可能方式?

4

2 回答 2

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);
于 2013-09-24T18:18:14.173 回答
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 将包含您的字符串。

于 2013-09-24T18:13:47.930 回答