C#字符串以f浮动 [英] C# String to float with f
问题描述
顺便说一句:xposs是从服务器接收的,所以我不能只说它是浮点数,我需要将其转换为后面带有0.0f的浮点数:
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);
这只返回浮点数951.9791,但我要951.9791f
this just returns 951.9791 in a float but i want 951.9791f
我也尝试过:
xposs = "951.9791"
xspawn = float.Parse(yposs) + 0.0f;
这仍然行不通.
我需要将其设置为0.0f格式,因为我在Vector3()中使用了它,但是没有f似乎没有用(很好,但是坐标完全错误,如果我在它之后手动按f有效.)
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.)
推荐答案
f
后缀仅适用于代码中的文字.它用于将文字值表示为float
.如果不存在后缀-文字值将为double
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
您可以将float.Parse(stringValue)
的结果作为Unity 3d中的构造函数参数传递给Vector3
和Vector2
接下来的代码创建一个Vector3对象,其x y z等于在字符串
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
这篇关于C#字符串以f浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!