2016-08-03 76 views
1

我就需要從文件中分析數據,並存儲到MySQL數據庫 一個項目文件有多種格式C#多個不同格式

Ava. bytes -> 147.258.369 
Ava. bytes -> 147.258.369,5 
Ava. bytes -> 147,258,369 
Ava. bytes -> 147,258,369.5 

什麼是最好的方式下面的字符串將這些格式轉換爲

Ava. bytes -> 147.258.369 => 147258369.0 
Ava. bytes -> 147.258.369,5 =>147258369.5 
Ava. bytes -> 147,258,369 => 147258369.0 
Ava. bytes -> 147,258,369.5 =>147258369.5 

謝謝!

+1

是十進制永遠只有第十?因爲如果你有147.258和147,258 ......你沒有足夠的信息來知道你的意思是147.258還是147,258.000 – Ryan

+1

什麼是'123,456'? – SimpleVar

+0

這是固定寬度的數據,你可以依賴小數(如果有的話)總是在同一個位置? – DVK

回答

0

您可以使用正則表達式來刪除後跟三個數字的任何點或逗號,和一個字符串替換替換最終小數逗號一段,如果有一個:

string n = "147.258.369,5"; 
var r = new Regex(@"[.,](?=\d{3})"); 
string n2 = r.Replace(n, "").Replace(',', '.'); 

編輯:(但想必你想要的字符串,因爲.0很重要)

的字符串是那麼InvariantFormat,如果你想將它解析爲十進制

要包括最後.0

if (n2.Length < 2 || n2[n2.Length - 2] != '.') 
{ 
    n2 = n2 + ".0"; 
} 
+0

我承認這個3班輪班比我的多班輔助班更有吸引力。但我強烈建議你避免使用正則表達式,但這並非絕對必要。正則表達式在這裏是一種矯枉過正的行爲,它會打破性能。 – SimpleVar

+0

@SimpleVar這是一個公平點,但這個正則表達式不需要做太多的工作(即最小的回溯)。這可能是一個優雅的解決方案,在實踐中足夠快。 –

0

我基本上通過評論, 解釋解決方案,所以你可以只讀出評論,並得到的想法。

注意我使用decimal作爲最終結果。但如果您願意,您可以使用floatdouble。一般方法仍然有效。

string str = "123,456.7"; 

// Check if its a simple integer (no floating point or thousands separators) 
if (str.Length <= 3) 
    return (decimal)int.Parse(str); 

// Get the floating-point character from the string 
char floatingPointChar = str[str.Length - 2]; 

// Based on the floating-point character, 
// we assure the string is in a standard format of "000,000.0" 
switch (floatPointChar) 
{ 
    case '.': 
     // If the floating point is a . 
     // the number is already in a standard format 
     break; 
    case ',': 
     // put ~ as a temporary floating-point character 
     str = str.Replace(',', '~'); 

     // turn dots into thousand separators 
     str = str.Replace('.', ','); 

     // put . as floating-point character 
     str = str.Replace('~', '.'); 
     break; 
    default: 
     // There is actually no floating point, 
     // so just make sure thousand separators are , 
     str = str.Replace('.', ','); 
     break; 
} 

// Now our string is in a standard format 
// , for thousands separators 
// . for floating point 
return decimal.Parse(str, CultureInfo.InvariantCulture); 

編輯:我完全忽略了一個事實,即千個分隔符可以完全省略。見空的答案更多的靈感

0

這是做的非常討厭的方式:

private static string FormatIntString(string input) 
    { 
     if (input.IndexOf('.') != input.LastIndexOf('.')) 
     { 
      if (input.Contains(",")) 
      { 
       //this case-> Ava.bytes -> 147.258.369,5 =>147258369.5 
       return DoFormat(input.Replace(".", "").Replace(',', '.')); 
      } 
      else 
      { 
       // this case-> Ava.bytes -> 147.258.369 => 147258369.0 
       return DoFormat(input.Replace(".", ""));     
      } 
     } 
     else 
     { 
      if (input.Contains('.')) 
      { 
       //this case -> Ava.bytes -> 147,258,369.5 =>147258369.5 
       return DoFormat(input.Replace(",", ""));     
      } 
      else 
      { 
       //this case -> Ava.bytes -> 147,258,369 => 147258369.0 
       return DoFormat(input.Replace(",", "")); 
      } 
     } 
    } 
    public static string DoFormat(string myNumber) 
    { 
     var s = string.Format("{0:0.00}", myNumber); 
     if (s[s.Length-2] != '.')    
      return (myNumber + ".0");    
     else    
      return s;    
    } 

請記住,這僅適用於字符串至少有兩個「」或' '。

簡化代碼:

private static string FormatIntString(string input) 
    { 
     if (input.IndexOf('.') != input.LastIndexOf('.')) 
      if (input.Contains(",")) 
       return DoFormat(input.Replace(".", "").Replace(',', '.')); 
      else 
       return DoFormat(input.Replace(".", "")); 
     else    
      if (input.Contains('.')) 
       return DoFormat(input.Replace(",", "")); 
      else 
       return DoFormat(input.Replace(",", ""));    
    } 

    public static string DoFormat(string myNumber) 
    { 
     var s = string.Format("{0:0.00}", myNumber); 
     if (s[s.Length - 2] != '.') 
      return (myNumber + ".0"); 
     else 
      return s; 
    }