2011-12-20 65 views
3

我試圖從一個asc文件加載一些XY座標。它看起來像這樣:解析後錯誤的小數位數

-55.988544 9382
-53.395804 9403
-50.804601 9433

然後我轉換座標浮動。但不知何故f.e.對於第一個值我得到「-55988544.0」而不是「-55.988544」

下面是代碼:

private void btngettext_Click(object sender, EventArgs e) 
    { 
     StreamReader objStream = new StreamReader("C:\\...\\.asc"); 

     firstLine = objStream.ReadLine(); 

     int i = 0; 

     /*Split String on Tab, 
     * will separate words*/ 
     string[] words = firstLine.Split('\t'); 

     richTextBox1.Text = words[0]; 

     foreach(string word in words) 
     { 
      if(word != "") 
      { 
       Console.WriteLine(word); //the value of the string is "-55.988544" here 
       //value = float.Parse(word); tried both 
       value = Convert.ToSingle(word); //here the float value is "-55988544.0" 
       Console.WriteLine(value.ToString());// "-5,598854E+07" 
       xyArray[0,i] = value; 

       i++; 
      } 
     } 

    } 

此外,如果我會用objStream.ReadToEnd()或.Read(),怎麼可能通過線迭代。讀取第一行的值,保存並繼續下一行。

由於提前,

BC++

+0

驗證您的文化設置,特別是千位分隔符。 – 2011-12-20 19:34:49

+1

它正在治療。作爲千位分隔符,由於您的區域設置而不是小數點 – 2011-12-20 19:34:50

+0

感謝您的回覆。仍然沒有人知道如何迭代槽線並始終讀出座標對? – 2011-12-21 08:25:11

回答

6

聽起來好像您的應用程序正在「。」文化中運行。是千位分隔符而不是小數點分隔符。如果源文件總是使用,那麼它是更好地與解析「‘:

float.Parse(word, System.Globalization.CultureInfo.InvariantCulture); 

這將確保解析使用’。」不管機器文化是什麼。

3

這可能是與文化設置的問題。請嘗試以下操作:

var culture = System.Globalization.CultureInfo.InvariantCulture; 
value = float.Parse(word, culture); 
2

如果您的數據使用小數分隔符等的已知固定符號,則不應該依賴讀數PC的默認值。使用

value = float.Parse(word, CultureInfo.InvariantCulture);