Get only the first 10 numbers of X length

I have a method that calculates the distance between two xyz cords and returns me a nice long double number, e.g. 10,12345678963235. But I need only 10,12345, this would be enough for me. How can i do this? This is the method that returns me the value:

public static double Distance(Vector3 v1, Vector3 v2)
{        
    return
    (
        Math.Sqrt
        (
            (v1.X - v2.X) * (v1.X - v2.X) +
            (v1.Y - v2.Y) * (v1.Y - v2.Y) +
            (v1.Z - v2.Z) * (v1.Z - v2.Z)
        )
    );
}

Thank!

+3
source share
4 answers

. . , 15 . , - . , .

, . , , . :

Console.WriteLine("{0:N5}", value);

string output = value.ToString("N5");
+4
Math.Round(val, 5);
+4

, , , , - , 10 , double.

( , a ., ), , .


Math.Round(
    Math.Sqrt 
    ( 
        (v1.X - v2.X) * (v1.X - v2.X) + 
        (v1.Y - v2.Y) * (v1.Y - v2.Y) + 
        (v1.Z - v2.Z) * (v1.Z - v2.Z) 
    ), 5);

http://msdn.microsoft.com/en-us/library/aa340228.aspx

+3
source

There is no floating point encoding for exactly N decimal digits of precision. Use a decimal value if you want to.

If you need only 7-digit precision, then return a float that has exactly that.

public static float Distance(Vector3 v1, Vector3 v2)
{        
    return
    (
        (float)Math.Sqrt
        (
            (v1.X - v2.X) * (v1.X - v2.X) +
            (v1.Y - v2.Y) * (v1.Y - v2.Y) +
            (v1.Z - v2.Z) * (v1.Z - v2.Z)
        )
    );
}

If you want to display 7 digits of precision, then: result.ToString ("F7");

0
source

Source: https://habr.com/ru/post/1762077/


All Articles