• 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").Values the value short. How do I force it to return the absolute float in the document?

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?

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

  • 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!

This topic is now closed to further replies.
  • Posts

    • Display Driver Uninstaller (DDU) 18.1.2.3 by Razvan Serea Display Driver Uninstaller (DDU) is a utility for completely removing AMD/NVIDIA/INTEL graphics drivers and related packages from your system, attempting to eliminate all leftovers (including registry entries, folders and files, driver store). Though AMD/NVIDIA/INTEL drivers can usually be removed via the Windows Control Panel, this uninstaller tool was created for situations where standard uninstall fails, or when you need to fully remove NVIDIA or ATI graphics card drivers. After using this driver cleaner, your system will behave as though it’s the first time you’re installing a new driver—similar to a fresh Windows installation. As with all such tools, we recommend creating a restore point beforehand, allowing you to undo changes if issues arise. If you're having trouble installing an older or newer driver, try it—there are reports that it resolves such problems. Recommended usage: The tool can be used in Normal mode but for absolute stability when using DDU, Safemode is always the best. Make a backup or a system restore (but it should normally be pretty safe). It is best to exclude the DDU folder completely from any security software to avoid issues. You do NOT need to uninstall the driver prior using DDU. Requirements: .NET Framework 4.8 Compatible with Windows 7, 8, 8.1, 10, and 11 (32-bit or 64-bit) Note: Using on Insider Preview builds is at your own risk. Display Driver Uninstaller (DDU) 18.1.2.3 changelog: All GPUs: Fixed hardware rescan issue when cleaning all GPUs without restarting or shutting down UI: Added Guru3D mention in the About section. General: Various fixes and enhancements. Download: Display Driver Uninstaller (DDU) 18.1.2.3 | 1.7 MB (Freeware) Download: DDU Portable | 1.2 MB Links: Display Driver Uninstaller Home Page | Screenshot | Forum Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • That would be nice, if excel could refresh pivot tables without bugs. Frequently you refresh a pivot and all parameters get lost. Now this will happen automatically. Bravo MS!
    • If one could ever put all the crap of the world in one box, that would be Microsoft.
  • Recent Achievements

    • Week One Done
      Homayoun Hotak earned a badge
      Week One Done
    • Dedicated
      Profit earned a badge
      Dedicated
    • One Month Later
      hhgygy earned a badge
      One Month Later
    • Week One Done
      hhgygy earned a badge
      Week One Done
    • One Year In
      NIKI77 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      648
    2. 2
      ATLien_0
      241
    3. 3
      Xenon
      168
    4. 4
      neufuse
      148
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!