将数字转换为Facebook喜欢计数器
本文关键字:喜欢 计数器 Facebook 数字 转换 | 更新日期: 2023-09-27 18:32:48
这是我当前的代码:
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"
有没有像脸书计数器这样的方法?
这是我对此类问题的解决方案:
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);
以下是您想要什么的一般想法:
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 将包含您的字符串。