Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

C# String to float with f

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

f postfix is applied only to literals in code. It is used to denote literal values as float. If no postfix is present - literal value will be of type double

you can pass the result of float.Parse(stringValue) into Vector3 and Vector2 as constructor arguments in Unity 3d The next code create a Vector3 object with x y z equal to value set in string

string sideSize = "12.5";
float vectorSide = float.Parse(sideSize, CultureInfo.InvariantCulture);
Vector3 resultVector = new Vector3(vectorSide, vectorSide, vectorSide); //create vector3 with x = y = z = 12.5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...