• 0

[C#] XDocument shows full float value, but does not = the full value?


Question

So when I try to retrieve the full gps coordinates from my class I get the correct values. When I am looking at the debugger it shows the xml document contains the right values. However when I call float myVar = locationElem.Element("lat") or float myVar = float.Parse(locationElem.Element("lat").Value); it trims the value short. How do I force it to return the absolute float in the document?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

To clarify:


string status = xDoc.Element("GeocodeResponse").Element("status").Value;
            XElement locationElem = xDoc.Element("GeocodeResponse").Element("result").Element("geometry").Element("location");
            double testLat = (double)locationElem.Element("lat");
            double testLong = (double)locationElem.Element("lng");
            float latitude = (float)float.Parse(locationElem.Element("lat").Value);
            float longitude = (float)float.Parse(locationElem.Element("lng").Value);

locationElem.Value (for example) = 22.01919203 but the float is stored as 22.0191...o_O? What can I do to get the full 22.0191203?

Link to comment
Share on other sites

  • 0

That's probably because floats don't have enough precision to store that many significant decimal places. You should use a decimal instead. Btw you should always specify CultureInfo.InvariantCulture to those Parse methods otherwise they will fail on any machine that has a different decimal separator.

Link to comment
Share on other sites

  • 0

Thanks Dr_Asik :) helpful as always. +1 for you (I think there is an up vote button on here? I'll find it :) ).

EDIT: There isn't one for an individual post so I rated you 5 stars on your profile. I was banging my head on my desk about this one. Google even shows an example using floats so I was a bit confused as to why my example wasn't working like theirs was. I'm going to try the decimal idea though. Lets hope for the best!

Link to comment
Share on other sites

This topic is now closed to further replies.