views:

107

answers:

4

Hello all. 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!

+3  A: 

After re-reading the question, I think the only option you have is to convert to a string, take the first 10 characters, then convert back to double.

If your number is a decimal (you've got a , not a . so I'm not sure) the method below should work, otherwise you'll need to do the string thing.


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

Nate Bross
How can I combine the two? I need Math.Sqrt too.I use it now like this:this.distance_label.Text = "Distance between player and target : " + Math.Round(Vector3.Distance(PlayerPos, TargetPos),7).ToString();I think its a little bit dirty :D
Galaris
You could put the call to `Round` inside the Distance method.
Nate Bross
+3  A: 
Math.Round(val, 5);
Femaref
Thank you too!!
Galaris
A: 

There's no floating point encoding for exactly N decimal digits of precision. Use decimal if you want that.

If you only want 7 digits precision stored then return a float, which conveniently has just 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");

mancaus
thank you too :) I've learned a lot.
Galaris
+3  A: 

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:

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

or

string output = value.ToString("N5");
Hans Passant
I have to disagree. When a number is supposed to be managed to a certain degree of accuracy you should round (Or truncate) to that. As an example a piece of software used in my industry keeps 6 significant digits after the decimal point for currency but only displays the normal 2. This causes round off errors and invoices where the total does not appear to match the sum of the line items.
Mike B
@Mike: I have to disagree. Floating point values model physical numbers. Nature does not count with 10 fingers, only humans do. There is no universal constant that can be rounded to a nice number and still be accurate. A laden swallow does not fly at 10.12345 miles per hour, it is clueless about a mile or an hour. There is however a class of calculations that *do* desire powers of 10. Accountants are picky about it. But those kind of calculations should *always* use System.Decimal, *never* double or float.
Hans Passant
Perhaps, I was refering to the stressed "Never" in your response. also, is that a European or African swallow?
Mike B
Oops, Unladen of course.
Hans Passant