Search Unity

How can I convert a string to a float?

Discussion in 'Scripting' started by ruibjr, Jan 9, 2010.

  1. ruibjr

    ruibjr

    Joined:
    May 30, 2009
    Posts:
    43
    I have searched, but I haven't found anything that works for me.

    Here's what's happening:

    I have a string which holds several parameters, this string is retrieved from a database, but for testing purposes, I am loading it directly.

    Code (csharp):
    1.  
    2. var theSolution = "0,0,0,-1000,-0.001,0,0,0,1;0,200,600,70,1000,0,0,3,1;0,100,400,50,1000,0,0,2,1;0,100,200,30,1000,0,0,2,1;0,0,50,0,0,0,0,1,1;1,0,0,0,0,0,0,0,0;1,0,650,0,0,0,0,2,0;1,0,1000,0,0,0,0,2,1;";
    3.  
    Then, I call the following function, in order to transfer those values into a bidimensional float array, for later use.

    Code (csharp):
    1.  
    2. function parseSolution() {
    3.     var i : Number;
    4.     var j : Number;
    5.     var theRows : Array = theSolution.Split(";"[0]);
    6.     var theColumns : Array;
    7.     var L :Number = theRows.length;
    8.     var M:Number;
    9.     for(i = 0; i < L; i++) {
    10.         theColumns = theRows[i].Split(","[0]);
    11.         M = theColumns.length;
    12.         for(j = 0; j < M; j++) {
    13.             theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
    14.         }
    15.     }
    16. }
    17.  
    At the top of the script, I have created the array, using this:

    Code (csharp):
    1.  
    2. private var theConstraints = MultiDim.FloatArray(10, 10);
    3.  
    The problem is, I have't found a way to convert the strings to floats, at the parsing loop (more specifically, this line):

    Code (csharp):
    1.  
    2.             theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
    3.  
    It keeps giving me this error:

    FormatException: Invalid format.
    System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider)
    System.Single.Parse (System.String s)
    Land.parseSolution () (at Assets\Land.js:49)
    Land.Start () (at Assets\Land.js:28)

    Is there a simple, reliable way to convert strings to floats, in Unity?
    I mean, this should not be difficult, right?
    Besides, how come there is no easy way to create a 2-dimensional array of floats?

    Cheers,

    Rui
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not difficult, but you're passing it something it can't parse, so naturally it's going to give you that error. Before the float.Parse line, try this:

    Code (csharp):
    1. print (theColumns[j]);
    and you'll see what the problem is. You can deal with that by using try/catch, or TryParse, or being sure you aren't passing invalid data to begin with. (To make it even more obvious, do "for (row in theRows) print(row);".)

    By the way, "Number" is an alias for System.Double. Since there's no point using double-precision floating-point for those variables, what you actually want is "int" (which is an alias for System.Int32). Better yet, you can let type inferencing take care of it all:

    Code (csharp):
    1.    var theRows = theSolution.Split(";"[0]);
    2.    var L = theRows.length;
    3.    for(i = 0; i < L; i++) {
    4.       var theColumns = theRows[i].Split(","[0]);
    5.       M = theColumns.length;
    6.       for(j = 0; j < M; j++) {
    7.          theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
    8.       }
    9.    }
    That will actually run better.

    I assume you got the MultiDim class from here, which explains it. That's as easy as it gets until UT fixes Javascript to be able to do it directly.

    --Eric
     
  3. ruibjr

    ruibjr

    Joined:
    May 30, 2009
    Posts:
    43
    Thanks for the reply.

    While looking for a solution, I came up with this "myParseFloat", which worked fine.


    Code (csharp):
    1.  
    2. function myParseFloat(v : String) {
    3.     var sgn = 1;
    4.     var p = 0;
    5.     var i = 0;
    6.     var f = 0;
    7.    
    8.     if(v == "") return 0;
    9.     if(v.Substring(0, 1) == "-") {
    10.         sgn = -1;
    11.         v = v.Substring(1);
    12.     }
    13.     p = v.IndexOf(".");
    14.     if(p < 0) {
    15.         return sgn * parseInt(v);
    16.     } else {
    17.         i = parseInt(v.Substring(0, p));
    18.         f = parseInt(v.Substring(p + 1));
    19.         return sgn * (i + (f / (Mathf.Pow(10, (v.length - p - 1)))));
    20.     }
    21. }
    22.