I would like to convert a string to float in order to do math operations on it.
Here is my line of code where I want to convert the string, which is stored in an array to a float:
var mensAverageScore : Float = arr[0] / arr[1];
I have tried the following with no luck:
var mensAverageScore : Float = parseFloat(arr[0]) / parseFloat(arr[1]);
Any ideas?
THanks,
Chris
UPDATE:
I have tried the following with no luck, and placed all my code.
//Here just reads text file and place everything in an array
var regex : String = ",";
var arr : Array = Regex.Split(downloadText,regex);
//Tested here, prints arr[0] and arr[1] fine
//Now just want to convert them into float so i can do some math
var mensTotalTimes : float = float.TryParse(arr[0]);
var mensTotalTries : float = float.TryParse(arr[1]);
//Printing mensAverageScore doesnt work
var mensAverageScore : float = mensTotalTimes / mensTotalTries ;
/*** THIS WORKS ***/
var mensTotalTimes : float = parseFloat(arr[0]);
var mensTotalTries : float = parseFloat(arr[1]);
I basically just need to cast the String to an Int or Float. I know in PHP this is done pretty easily, but can't seem to find how to do it here.
UNITY 4 COMPILED BUT DIDNT WORK THIS PAGE SOLUTIONS: var yodastring : String = "5"; yodamod= parseFloat( yodastring); var yodastring : String = "5"; yodamod= parse.Float( yodastring); var yodastring : String = "5"; yodamod= parse.float( yodastring); var yodastring : String = "5"; yodamod= 1yodastring;* yodamod= float.TryParse( yodastring); not compiled PLEASE HAVING A WORKING CODE IN :) SOLUTION: STRING VARIABLE MUST BE PRIVATE VAR.
You should use float.Parse or float.TryParse (float is the same as Single). However, depending on your culture settings, floats can be represented with a comma (,) or dot (.) to separate the decimals.
If you want to use the following format "3.14" (as perhaps opposed to "3,14"), use the invariant culture number format System.Globalization.CultureInfo.InvariantCulture.NumberFormat.
var mensTotalTries : float = float.Parse(arr[1],
System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
Also make sure to use the same culture info when converting floats back to strings since they will use your computers culture info by default (causing 3.14 to become "3,14"). Make sure to call ToString like this to get string "3.14".
It's worth to mention that float or Single has preciseness of 7 digits, means if your number string has more digits it will be rounded. alternatively you could use Double.Parse(). A Double will be rounded at 15 digits. Don't forget to use correct type for your variables like var myNumber : Double;
You want to use "float", not "Float". Aside from parseFloat(), there's also float.TryParse(), which can handle invalid input better.
Thank you very much for replying. I have tried what you said and it didn't work. I updated the question with the code I am using implementing it with the TryParse(). Put it up there so it's easier to read.
It will throw up errors if the string is not private var. segmod = float.Parse( segstring ); worked with private var string.
That is interesting. There should be compile time errors in both sets of signatures. I checked under Visual Studio 2015 as well as MonoDevelop and do see compiler errors of the type you listed earlier in both cases. For Visual Studio ensure the error filter within the Error List window is not suppressed else no errors will be listed even though errors are generated.
I basically just need to cast the String to an Int or Float. I know in PHP this is done pretty easily, but can't seem to find how to do it here.
– cmosUNITY 4 COMPILED BUT DIDNT WORK THIS PAGE SOLUTIONS: var yodastring : String = "5"; yodamod= parseFloat( yodastring); var yodastring : String = "5"; yodamod= parse.Float( yodastring); var yodastring : String = "5"; yodamod= parse.float( yodastring); var yodastring : String = "5"; yodamod= 1yodastring;* yodamod= float.TryParse( yodastring); not compiled PLEASE HAVING A WORKING CODE IN :) SOLUTION: STRING VARIABLE MUST BE PRIVATE VAR.
– frogsbo