Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by cmos · Feb 16, 2010 at 01:14 AM · math

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Feb 16, 2010 at 03:32 AM 1
Share

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.

avatar image frogsbo · Jun 09, 2014 at 02:17 PM 0
Share

UNITY 4 CO$$anonymous$$PILED BUT DIDNT WOR$$anonymous$$ 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= 1*yodastring;

yodamod= float.TryParse( yodastring); not compiled

PLEASE HAVING A WOR$$anonymous$$ING CODE IN :)

SOLUTION: STRING VARIABLE $$anonymous$$UST BE PRIVATE VAR.

6 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Eric5h5 · Feb 16, 2010 at 03:40 AM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Feb 16, 2010 at 03:48 AM 0
Share

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.

avatar image cmos · Feb 16, 2010 at 03:59 AM 0
Share

Thanks!!! It works now. Just changed it to parseFloat ins$$anonymous$$d. Thanks, stuck for 3 hours on this.

avatar image Eric5h5 · Feb 16, 2010 at 04:04 AM 0
Share

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

avatar image
10

Answer by Statement · Feb 16, 2010 at 09:50 AM

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image synapsemassage · Jun 09, 2011 at 08:09 PM 0
Share

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;

avatar image
3

Answer by Ashkan_gc · Feb 16, 2010 at 05:03 AM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image donskyale_412 · Sep 24, 2011 at 01:49 AM 0
Share

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?

avatar image frogsbo · Jun 09, 2014 at 02:11 PM 0
Share

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

avatar image
1

Answer by donskyale_412 · Sep 23, 2011 at 03:34 PM

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

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by synapsemassage · Sep 24, 2011 at 09:15 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frogsbo · Jun 09, 2014 at 02:09 PM 0
Share

that seems obsolete in v4

avatar image frogsbo · Jun 09, 2014 at 02:27 PM 0
Share

no the string has to be private var

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

More simple vector math... 2 Answers

Radar script help 2 Answers

Math Optimization: Normalize 2 Answers

How do I seed the random number generator myself and still get pseudorandom values? 2 Answers

Converting an Azimuthal Coordinate to Vector3D? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges