[英]Formatting Large Numbers with .NET
I have a requirement to format large numbers like 4,316,000 as "4.3m". 我要求将4,316,000这样的大数字格式化为“4.3m”。
How can I do this in C#? 我怎么能在C#中做到这一点?
You can use Log10 to determine the correct break. 您可以使用Log10来确定正确的中断。 Something like this could work:
像这样的东西可以工作:
double number = 4316000;
int mag = (int)(Math.Floor(Math.Log10(number))/3); // Truncates to 6, divides to 2
double divisor = Math.Pow(10, mag*3);
double shortNumber = number / divisor;
string suffix;
switch(mag)
{
case 0:
suffix = string.Empty;
break;
case 1:
suffix = "k";
break;
case 2:
suffix = "m";
break;
case 3:
suffix = "b";
break;
}
string result = shortNumber.ToString("N1") + suffix; // 4.3m
divide the number by 1000000.0, then append an "m". 将数字除以1000000.0,然后加上“m”。
remember to round the number to 1 decimal place. 记得将数字四舍五入到小数点后1位。
public static class Program
{
private static void Main(string[] args)
{
double[] numbers =
{
3000, 3300, 3333, 30000, 300000, 3000000, 3000003, 0.253, 0.0253, 0.00253, -0.253003
};
foreach (var num in numbers)
{
Console.WriteLine($"{num} ==> {num.Humanize()}");
}
Console.ReadKey();
}
public static string Humanize(this double number)
{
string[] suffix = {"f", "a", "p", "n", "μ", "m", string.Empty, "k", "M", "G", "T", "P", "E"};
var absnum = Math.Abs(number);
int mag;
if (absnum < 1)
{
mag = (int) Math.Floor(Math.Floor(Math.Log10(absnum))/3);
}
else
{
mag = (int) (Math.Floor(Math.Log10(absnum))/3);
}
var shortNumber = number/Math.Pow(10, mag*3);
return $"{shortNumber:0.###}{suffix[mag + 6]}";
}
}
This should output: 这应输出:
3000 ==> 3k
3300 ==> 3,3k
3333 ==> 3,333k
30000 ==> 30k
300000 ==> 300k
3000000 ==> 3M
3000003 ==> 3M
0,253 ==> 253m
0,0253 ==> 25,3m
0,00253 ==> 2,53m
-0,253003 ==> -253,003m
long valueToFormat = 4316000;
var dict = new Dictionary<long, string>() {
{1000000000, "b"},
{1000000, "m"},
{1000, "k"}
};
string formattedValue = valueToFormat.ToString();
foreach (long n in dict.Keys.OrderBy(k => k)) {
if (valueToFormat < n) {
continue;
}
double value = Math.Round(valueToFormat / (double)n, 1);
formattedValue = String.Format("{0}{1}", value, dict[n]);
}
Console.WriteLine(formattedValue);
If you're only running on Windows you could use a p-invoke declaration in C# or VB.NET to call the Win32 functions StrFormatByteSizeW or StrFormatByteSize64 . 如果您只在Windows上运行,则可以在C#或VB.NET中使用p-invoke声明来调用Win32函数StrFormatByteSizeW或StrFormatByteSize64 。 If your application/site is guaranteed to be running on at least Vista SP1 or Server 2008 there's also StrFormatByteSizeEx with a few more options.
如果您的应用程序/站点保证至少在Vista SP1或Server 2008上运行,那么还有一些StrFormatByteSizeEx以及更多选项。
Sample from the MSDN docs: 来自MSDN文档的示例:
Numeric value Text string
532 532 bytes
1340 1.30KB
23506 22.9KB
2400016 2.29MB
2400000000 2.23GB
These APIs also handle localization correctly for non-English users. 这些API还可以为非英语用户正确处理本地化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.