我需要将像 4,316,000 这样的大数字格式化为4.3m".
I have a requirement to format large numbers like 4,316,000 as "4.3m".
如何在 C# 中做到这一点?
How can I do this in C#?
你可以使用 Log10 来确定正确的中断.像这样的东西可以工作:
You can use Log10 to determine the correct break. 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
这篇关于使用 .NET 格式化大数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!