簡體   English   中英

將十進制字符串解析為字節

[英]Parsing a decimal string to a byte

我在將十進制字符串解析為字節時遇到問題。

這是帶有位置數據的數組,通常是整數。但是我已經能夠創建一種位置數據為小數的情況,這會使服務器崩潰,因為我似乎無法解析它。

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

這行代碼給我一個錯誤,提示輸入格式不正確:

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

我一直在嘗試使用Math.Round而不成功。 我確實認為我必須四舍五入小數,因為我正在檢索X和Y來放置家具單元。

另外,locationData [1] = 6.0000的內容,這是它試圖解析的數字/字符串。

我似乎無法解決問題,希望您能有所幫助。

嘗試在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