简体   繁体   中英

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. I do think I have to round the decimals, because I'm retrieving the X and Y to place furniture unit.

Also, the content of locationData[1] = 6.0000, which is the number/string it's trying to parse.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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