Before updating to Windows 10 you may need to return your Pro license. More information here

Convert String to Float


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


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]);

more ▼

asked Feb 16, 2010 at 01:14 AM

avatar image

602 75 73 87

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.

Feb 16, 2010 at 03:32 AM cmos


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= 1*yodastring;

yodamod= float.TryParse( yodastring); not compiled



Jun 09, 2014 at 02:17 PM frogsbo
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

You want to use "float", not "Float". Aside from parseFloat(), there's also float.TryParse(), which can handle invalid input better.

more ▼

answered Feb 16, 2010 at 03:40 AM

avatar image

121k 80 365 774

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.

Feb 16, 2010 at 03:48 AM cmos

Thanks!!! It works now. Just changed it to parseFloat instead. Thanks, stuck for 3 hours on this.

Feb 16, 2010 at 03:59 AM cmos

See http://msdn.microsoft.com/en-us/library/26sxas5t.aspx for info on how to use TryParse().

Feb 16, 2010 at 04:04 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

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.

See this post about number formats.

var mensTotalTries : float = float.Parse(arr[1],

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

more ▼

answered Feb 16, 2010 at 09:50 AM

avatar image

28.2k 82 184 307

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;

Jun 09, 2011 at 08:09 PM synapsemassage
(comments are locked)
10|3000 characters needed characters left

use float.parse to convert the string to float. also int and double and ... has a parse method too.

more ▼

answered Feb 16, 2010 at 05:03 AM

avatar image

10.5k 78 127 171

I tried float.parse and also float.TryParse but none of them worked and it affected the whole program...I also tried other codes from other source but none worked...Is it really possible to convert using javascript?

Sep 24, 2011 at 01:49 AM donskyale_412

same problem here . didng get a solution from this page in unity4

Jun 09, 2014 at 02:11 PM frogsbo
(comments are locked)
10|3000 characters needed characters left

sorry to ask this question but i'm a new user...

I tried it but the error message was "cannot convert string to float";

more ▼

answered Sep 23, 2011 at 03:34 PM

avatar image

16 7 8 9

(comments are locked)
10|3000 characters needed characters left

private var myFloat : float; private var myString : String: private var anotherFloat : float;

myFloat = myString * anotherFloat;

In Unity Javascript I simply cast a string to float like this and it works in my script without errors.

more ▼

answered Sep 24, 2011 at 09:15 AM

avatar image

516 7 12 29

that seems obsolete in v4

Jun 09, 2014 at 02:09 PM frogsbo

no the string has to be private var

Jun 09, 2014 at 02:27 PM frogsbo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 16, 2010 at 01:14 AM

Seen: 38930 times

Last Updated: Jun 09, 2014 at 02:28 PM