c# - 使用 .NET 格式化大数

标签 c# .net formatting numbers humanize

我需要将像 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/

相关文章:

c# - 通过 .editorconfig 将私有(private)方法的命名样式设置为驼峰式

python-3.x - Python打印具有给定位数的 float

c# - WPF 绑定(bind) : ! 值

c# - 使物体朝某个方向加速

c# - 如何在 Razor 中声明局部变量?

c# - CacheManager - 每 x 分钟或到期时刷新缓存

c# - 在 Winform 应用程序的 DataGridview 中隐藏底部的额外行

c - printf float 以尾随零的数量作为变量

php - HTML 按钮在页面刷新时移动?

c# - 在无服务器上下文中,C# async/await 有什么好处?