Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by CHHOST · Jan 04, 2013 at 10:33 PM · c#vector3stringfloat

string to float with f

So i have the following code (all is in C#)

BTW: the xposs is recieved from a server so i cant just say its a float, i need it converted to a float with 0.0f behind:

 xposs = "951.9791"
 
 xspawn = float.Parse(yposs);

this just returns 951.9791 in a float but i want 951.9791f

i have also tried:

     xposs = "951.9791"
     
     xspawn = float.Parse(yposs) + 0.0f;

this still does not work.

I need it to be 0.0f format becaouse i use it in a Vector3(), and not having the f does not seem to work (well it works but the cordinates are totally wrong, and if i manually press f after it works.)

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jan 04, 2013 at 10:41 PM

A float can't be 951.9791f, it can only be 951.9791. You only add the "f" in a script to tell the compiler that it's a float, but once compiled the number itself does not and cannot contain any letters. float.Parse is correct and returns a float, but your problem seems to be that you are using "xspawn = float.Parse(yposs);" when you should presumably be using "xspawn = float.Parse(xposs);".

Comment
Add comment · Show 5 · 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 CHHOST · Jan 04, 2013 at 10:44 PM 0
Share

Cant confirm that yor statement is true. I will see if i have miss typed the x y z. Ill get back tommorow.

avatar image Landern · Jan 05, 2013 at 12:29 AM 0
Share

I can confirm that Eric5h5 is not high and 100% correct. The reason a float contains the f at the end is to ensure it doesn't parse as a double. This isn't needed when you float.parse since there is no question that the floating point number is meant to be a float.

avatar image Eric5h5 · Jan 05, 2013 at 01:07 AM 0
Share

Why can't I be 100% correct and high? ;)

avatar image Owen-Reynolds · Jan 05, 2013 at 01:10 AM 0
Share

It's the same as how you say w="cows"; in the program, but you don't need the quotes when a user enters input, or you say w=w2;.

The f for float is like the quotes for strings.

EDIT: I assume Landern works at a drug-testing facility. Still, revealing results like that seems unethical.

avatar image Landern · Jan 07, 2013 at 05:40 PM 1
Share

Actually I have greater respect for Eric with that reply. :)

avatar image
1

Answer by Piflik · Jan 04, 2013 at 10:36 PM

No idea if that works, just a spontaneous idea...

 xposs = xposs + "f";
 
 xspawn = float.Parse(xposs);

Edit: Nope, doesn't work.

Another edit:

Your code works here. No need for an f...

 private string xposs = "951.9791";
 
 private float xspawn;
 
 void Start () {
     xspawn = float.Parse(xposs);
     print(xspawn);
     transform.position = Vector3.zero * xspawn;
 }

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 CHHOST · Jan 04, 2013 at 10:41 PM 0
Share

No it it does not. Unity starts the game, but when the functions is called i get tons of error saying it cant convert f to float :)

avatar image CHHOST · Jan 04, 2013 at 11:57 PM 0
Share

How can i put it in this:

New vector3(xposs, yposs, zposs) ?

Would that work?

avatar image
1

Answer by Mr.Z · Jan 04, 2013 at 10:47 PM

Floats are just a numeric value with type System.Float. They don't have an f behind it. That's just the notation C# uses for float literals. float.Parse returns a float, it's not possible or necessary to add an f.

[http://stackoverflow.com/questions/14166089/c-sharp-string-to-float-with-f][1] [1]: http://stackoverflow.com/questions/14166089/c-sharp-string-to-float-with-f
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

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I create a list with an int/float and a string? C# 2 Answers

Multiple Cars not working 1 Answer

Hiding decimals without using Mathf.Round? 2 Answers

Losing float precision when adding to Vector3 1 Answer

How to Display only current instead of current/max on a GUI.Box 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