|
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:
I have tried the following with no luck:
Any ideas? THanks, Chris UPDATE: I have tried the following with no luck, and placed all my code.
(comments are locked)
|
|
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.
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)
|
|
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.
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;
Jun 09 '11 at 08:09 PM
synapsemassage
(comments are locked)
|
|
use float.parse to convert the string to float. also int and double and ... has a parse method too. 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)
|
|
sorry to ask this question but i'm a new user... I tried it but the error message was "cannot convert string to float";
(comments are locked)
|
In Unity Javascript I simply cast a string to float like this and it works in my script without errors.
(comments are locked)
|

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.