我需要将像 4,316,000 这样的大数字格式化为“4.3m”。
如何在 C# 中做到这一点?
最佳答案
您可以使用 Log10来确定正确的休息时间。像这样的东西可以工作:
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
关于c# - 使用 .NET 格式化大数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1555397/