# How can I convert a string to a float?

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.

``````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;";
``````

Then, I call the following function, in order to transfer those values into a bidimensional float array, for later use.

``````function parseSolution() {
var i : Number;
var j : Number;
var theRows : Array = theSolution.Split(";"[0]);
var theColumns : Array;
var L :Number = theRows.length;
var M:Number;
for(i = 0; i < L; i++) {
theColumns = theRows[i].Split(","[0]);
M = theColumns.length;
for(j = 0; j < M; j++) {
theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
}
}
}
``````

At the top of the script, I have created the array, using this:

``````private var theConstraints = MultiDim.FloatArray(10, 10);
``````

The problem is, I have’t found a way to convert the strings to floats, at the parsing loop (more specifically, this line):

``````			theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
``````

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

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:

``````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:

``````   var theRows = theSolution.Split(";"[0]);
var L = theRows.length;
for(i = 0; i < L; i++) {
var theColumns = theRows[i].Split(","[0]);
M = theColumns.length;
for(j = 0; j < M; j++) {
theConstraints[i,  j] = float.Parse(theColumns[j], CultureInfo.InvariantCulture);
}
}
``````

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

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

``````function myParseFloat(v : String) {
var sgn = 1;
var p = 0;
var i = 0;
var f = 0;

if(v == "") return 0;
if(v.Substring(0, 1) == "-") {
sgn = -1;
v = v.Substring(1);
}
p = v.IndexOf(".");
if(p < 0) {
return sgn * parseInt(v);
} else {
i = parseInt(v.Substring(0, p));
f = parseInt(v.Substring(p + 1));
return sgn * (i + (f / (Mathf.Pow(10, (v.length - p - 1)))));
}
}
``````