Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用.NET格式化大数字_C#_.net_Formatting_Numbers_Humanize - Fatal编程技术网

C# 用.NET格式化大数字

C# 用.NET格式化大数字,c#,.net,formatting,numbers,humanize,C#,.net,Formatting,Numbers,Humanize,我需要将像4316000这样的大数字格式化为“4.3m” 如何在C#中执行此操作?将数字除以1000000.0,然后附加一个“m” 记住将数字四舍五入到小数点后1位。您可以用它来确定正确的中断。类似这样的方法可能会奏效: double number = 4316000; int mag = (int)(Math.Floor(Math.Log10(number))/3); // Truncates to 6, divides to 2 double divisor = Math.Pow(10,

我需要将像4316000这样的大数字格式化为“4.3m”


如何在C#中执行此操作?

将数字除以1000000.0,然后附加一个“m”

记住将数字四舍五入到小数点后1位。

您可以用它来确定正确的中断。类似这样的方法可能会奏效:

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
long valueToFormat=4316000;
var dict=新字典(){
{1000000000,“b”},
{1000000,“m”},
{1000,“k”}
};
string formattedValue=valueToFormat.ToString();
foreach(dict.Keys.OrderBy中的长n(k=>k)){
if(格式值
如果您仅在Windows上运行,则可以使用C#或VB.NET中的p-invoke声明来调用Win32函数或。如果您的应用程序/站点保证至少在Vista SP1或Server 2008上运行,那么还有一些其他选项

MSDN文档中的示例:

Numeric value   Text string 
532             532 bytes 
1340            1.30KB 
23506           22.9KB 
2400016         2.29MB 
2400000000      2.23GB 

这些API还可以为非英语用户正确地处理本地化问题。

嗯,编程语言?站台?帮帮我们。简短的回答是:编写一些代码,根据需求适当地格式化各种范围的值。@Jim:c#在问题的标签中。这个问题可以让问题变得更明显一些,你也可以加上标签钱或财务。。。这会帮助很多人:)@John Gietzen:微软雇佣巨魔作为程序经理?@MusiGenesis:看看微软的一些软件,这可能是事实:)如果它总是试图做数百万,这是可行的,但如果他想要数千、数百万、数十亿等等,就不行了。这个问题问的是4316000这样的数字是“430万”,其他格式不在要求中:)这不会像问题所问的那样将4316000作为“4.3m”,而是“4.12MB”Oops,将问题理解为关于二进制除数,而不是十进制除数。一定喜欢Occam的剃刀方法!
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]}";
    }
}
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]}";
    }
}
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