简体   繁体   English

C#数字格式:显示n位有效数字?

[英]C# number format: display n significant number of digits?

is there a way to format a double value such that it shows the n siginifacant digits only? 有没有办法格式化一个double值,使它只显示n个siginifacant数字?

for example I have a double with value 123456, can we have a format string such that it displays the first 3 digits only. 例如,我有一个值为123456的double,我们可以有一个格式字符串,只显示前3位数字。

double x=12346;
string s=x.ToString("Some format"); //display 123 only

is that possible? 那可能吗?

Although there are formats that would let you drop some or all of the fraction, no format would let you drop some of the significant digits of the whole part of the number. 虽然有些格式可以让你删除部分或全部分数,但是没有格式可以让你删除整个部分的一些有效数字。

If you would like to keep the first three digits of the whole number, you need to divide the value so that its whole part has only three digits. 如果您想保留整数的前三位数,则需要将该值除以使其整个部分只有三位数。

One approach to computing the divisor is taking log 10 N, checking if it is above 2, and dividing by 10 to the corresponding power: 计算除数的一种方法是记录10 N,检查它是否大于2,并将10除以相应的幂:

private static void Print(double x) {
    int n = (int)Math.Log10(x);
    if (n > 2) {
        x /= Math.Pow(10, n-2);
    }
    Console.WriteLine((int)x);
}

Demo. 演示。

You can't format the largest part of the double to only be to 3 sig. 你不能将double的最大部分格式化为3 sig。 fig. 图。 but you can split the string. 但你可以拆分字符串。 Try: 尝试:

String s = x.ToString().Substring(0, n);

Where n is the number of significant figures you wish to keep. 其中n是您希望保留的有效数字的数量。

I made a console application for this example. 我为这个例子做了一个控制台应用程序。 I know you need a double or int data type for the number, but I didn't know how manipulate the decimal numbers after the point, so I used a string (if you don't mind, store the number value inside string data type): 我知道你需要一个double或int数据类型的数字,但我不知道如何操纵点后的十进制数,所以我使用了一个字符串(如果你不介意,将数值存储在字符串数据类型中):

        string number = "";
        string digits = "";
        int n = 0;
        int count = 0;

        number = "45.6";
        //number = "456";
        n = 3;

        if (number.Contains('.')) //If the number has decimals...
        {
            if (n < number.Length)
            {
                if (number.IndexOf('.') < n)
                {
                    while (count <= n) //... we will count the number in a different way.
                    {
                        if (number[count] != '.')
                        {
                            digits = digits + number[count];
                        }

                        count++;
                    }
                }
                else
                {
                    while (count < n)
                    {
                        if (number[count] != '.')
                        {
                            digits = digits + number[count];
                        }

                        count++;
                    }
                }
            }
        }
        else
        {
            if (n <= number.Length)
            {
                while (count < n) //If not, we count without the decimal point.
                {
                    digits = digits + number[count];
                    count++;
                }
            }
        }

        Console.WriteLine("N significant digits: " + digits);

You can try with decimal and integer numbers, but in the code, they both are strings. 您可以尝试使用十进制和整数,但在代码中,它们都是字符串。 As I said before, if you don't mind using this data type, this example will help you, if not, you can try with "Substring" function from the String class. 正如我之前所说,如果您不介意使用此数据类型,此示例将帮助您,如果没有,您可以尝试使用String类中的“Substring”函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM