简体   繁体   中英

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?

for example I have a double with value 123456, can we have a format string such that it displays the first 3 digits only.

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:

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. 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.

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):

        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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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