Fabian Pas
Fabian Pas

Reputation: 444

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.

Upvotes: 2

Views: 571

Answers (1)

Akash KC
Akash KC

Reputation: 16310

Try specifying AllowDecimalPoint in NumberStyles and CultureInfo....

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

Upvotes: 4

Related Questions