I have a method , that calculates distance between two xyz cords, and return me a nice long double number like 10,12345678963235.
But I only need 10,12345 , that would be enought 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 you!
This is a very wrong-headed approach. You never want to intentionally throw accuracy away in your program. Your computer is patient, it has no trouble keeping track of up to 15 significant digits in a number. Which is as much as you’ll ever get out a double value. You’ll get in deep trouble if you intentionally make it less accurate and keep using the resulting values in further computations.
Only people care what the number looks like. Accommodate that by displaying the value to human eyes and then get rid of noise digits. Like:
or