Search Unity

String to float conversion of XML attribute

Discussion in 'Scripting' started by BCuracao, Jan 15, 2019.

  1. BCuracao

    BCuracao

    Joined:
    Apr 22, 2017
    Posts:
    17
    I am trying to convert a string value to a float value as follows:

    I pass a XML attribute as string to a converter class to get the associated value of the attribute as float

    Here I pass the "lat" attribute
    Code (CSharp):
    1. float lat = GetFloatAttributes("lat", node.Attributes);
    I use this class to get the value of the attribute back as float.
    Code (CSharp):
    1. public class AttributeFactory
    2. {
    3.     // Retrieves the value of a specific attribute
    4.     protected static float GetFloatAttributes(string value, XmlAttributeCollection attributeCollection)
    5.     {
    6.         // At this point the string has a value of "40.7801304"
    7.         string attribute = attributeCollection[value].Value;
    8.         // When the it returns the float value the value becomes something like "4.078013E+08"
    9.         return float.Parse(attribute);
    10.     }
    11. }
    The string attribute has a value of "40.7801304" when called in the GetAttributes function.
    But when the functions returns the value as float, the float lat has a value of "4.078013E+08"

    Snapshot of the xml node im trying to read like this:
    Code (CSharp):
    1.  <node id="42455666" visible="true" lat="40.7666184" lon="-73.9669476">
    2.   <tag k="highway" v="traffic_signals"/>
    3. </node>
    How can I fix that?
    The odd thing is that i copied this piece of code from an older Unity project (2017) in which it worked exactly that way.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Instead of float.Parse you could just use Convert.ToSingle (part of the System library) - I've no idea why float.Parse would have stopped working though.

    Perhaps what's happening is that the parser isn't recognising the decimal point, it could be a unicode decimal point or some other issue. You could also try this:

    (float)double.Parse(myStr,System.Globalization.NumberStyles.AllowDecimalPoint);
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Two thoughs.
    Try use
    float.tryparse
    the dot decimal point separator my not be liked. try use coma.
    There may be something related to 'culture', which referees, weather 1.356, is 1 thousands 3 hundreds, or 1 point 3.
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    A few throughts:
    • You can let the XML parser convert the string to a float directly by making the variable to serialize to a float.
    • To remove the culture, float number = float.Parse("0.54", CultureInfo.InvariantCulture);
    • Some rounding is to be expected because of the limited precision of a single precision float.
     
  5. BCuracao

    BCuracao

    Joined:
    Apr 22, 2017
    Posts:
    17
    Thanks you everyone for your help! I will try out your solutions as soon as im back home