将十进制字符串解析为字节

Fab*_*Pas 2 c#

我在将十进制字符串解析为字节时遇到问题。

这是带有位置数据的数组,通常是整数。但是我已经能够创建一种位置数据为小数的情况,这会使服务器崩溃,因为似乎无法解析它。

string[] locationData = Request.Content.Split(' ');
int itemID = int.Parse(locationData[0]);
byte newX = byte.Parse(locationData[1]);
byte newY = byte.Parse(locationData[2]);
Run Code Online (Sandbox Code Playgroud)

这行代码给我一个错误,提示输入格式不正确:

byte newX = byte.Parse(locationData[1]);
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用Math.Round而不成功。我确实认为我必须舍入小数点,因为我正在检索X和Y来放置家具单元。

同样,locationData [1] = 6.0000的内容,这是它试图解析的数字/字符串。

我似乎无法解决问题,希望您能有所帮助。

Aka*_* KC 5

尝试指定AllowDecimalPointNumberStylesCultureInfo....

byte newX = byte.Parse(locationData[1],NumberStyles.AllowDecimalPoint,CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)