x


Convert String to Float

Hi

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

asked Feb 16 '10 at 01:14 AM

cmos gravatar image

cmos
512 33 40 54

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 '10 at 03:32 AM cmos
(comments are locked)
10|3000 characters needed characters left

5 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 '10 at 03:40 AM

Eric5h5 gravatar image

Eric5h5
102k 49 165 619

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 '10 at 03:48 AM cmos

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

Feb 16 '10 at 03:59 AM cmos

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

Feb 16 '10 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],
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".

pi.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
more ▼

answered Feb 16 '10 at 09:50 AM

Statement gravatar image

Statement
25.3k 41 78 197

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 '11 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 '10 at 05:03 AM

Ashkan_gc gravatar image

Ashkan_gc
9.9k 36 59 123

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 '11 at 01:49 AM donskyale_412
(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 '11 at 03:34 PM

donskyale_412 gravatar image

donskyale_412
16 3 4 5

(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 '11 at 09:15 AM

synapsemassage gravatar image

synapsemassage
441 2 5 17

(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

By RSS:

Answers

Answers and Comments

Topics:

x324

asked: Feb 16 '10 at 01:14 AM

Seen: 25657 times

Last Updated: Sep 24 '11 at 09:17 AM