简体   繁体   English

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

[英]Parsing a decimal string to a byte

I'm having problems with parsing a decimal string to a byte. 我在将十进制字符串解析为字节时遇到问题。

This is the array with the location data, which normally are round numbers, but I've been able to create a situation where the location data is in decimals, this makes the server crash because it seems I can't parse it. 这是带有位置数据的数组,通常是整数。但是我已经能够创建一种位置数据为小数的情况,这会使服务器崩溃,因为我似乎无法解析它。

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

And this is the line which gives me an error saying the input isn't in the correct format: 这行代码给我一个错误,提示输入格式不正确:

byte newX = byte.Parse(locationData[1]);

I've been trying to use Math.Round without any succes. 我一直在尝试使用Math.Round而不成功。 I do think I have to round the decimals, because I'm retrieving the X and Y to place furniture unit. 我确实认为我必须四舍五入小数,因为我正在检索X和Y来放置家具单元。

Also, the content of locationData[1] = 6.0000, which is the number/string it's trying to parse. 另外,locationData [1] = 6.0000的内容,这是它试图解析的数字/字符串。

I can't seem to solve the situation and hope you could be of assistance. 我似乎无法解决问题,希望您能有所帮助。

尝试在NumberStylesCultureInfo ...中指定AllowDecimalPoint

byte newX = byte.Parse(locationData[1],NumberStyles.AllowDecimalPoint,CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM