2016-03-07 64 views
1

我在字符串識別中遇到問題:我試圖識別只有正確格式的數字,但沒有找到怎麼樣。避免decimal.TryParse將「1.1.1」或「1,1,1」識別爲十進制

我正在寫一種文化不變的方式,所以我需要識別「,」和「。」。作爲十進制和千位分隔符,反之亦然。

所有

這些都是正確的格式對我來說:

12,1 
12.1 
1.000,12 
1,000.12 

,但這樣的事情是錯誤的

1.2.3 
1,2,3 

我想:

NumberStyles style; 
decimal n; 
object valore;         
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands; 

Console.WriteLine(decimal.TryParse(valore.ToString(), style , CultureInfo.InvariantCulture, out n)); 

this is the fiddle to test it

但「1 ,1 ,1「被讀作有效數字,而」1.1.1「不被讀取。

如果我使用自己的特定文化(「IT-IT」),「1,1,1」被識別,「1.1.1」不被識別。

如何丟棄所有無效的字符串?

+2

據我可以告訴你沒有在代碼中的任何地方使用'style' – user9993

+0

你必須知道源格式提供者,否則這是一個不可能完成的任務。正如你自己所認識的那樣,兩者在特殊情況下(文化)都是有效的。 –

+0

@ user9993,代碼已更正,謝謝 –

回答

0

我弄成了個人的解析器,我希望能找到一個更好的解決方案,但它似乎很不可能

public static bool tryValoreNumerico(object valore, out decimal valoreRestituito) 
    { 
     decimal n;   
     string testoNormalizzato; 

     valoreRestituito = 0; 

     // normalizzazione 
     if (valore.ToString().Contains(",") && valore.ToString().Contains(".")) 
     { 
      if (valore.ToString().IndexOf(".") < valore.ToString().IndexOf(",")) 
      { 
       testoNormalizzato = valore.ToString().Replace(".", ""); 
      } 
      else 
      { 
       testoNormalizzato = valore.ToString().Replace(",", ""); 
      } 
     } 
     else 
     { 
      if ((valore.ToString().Length - valore.ToString().Replace(",", "").Length) > 1) 
      { 
       string[] pezzi = valore.ToString().Split(','); 
       for (int i = 1; i < pezzi.Length; i++) 
       { 
        if (pezzi[i].Length != 3) 
         return false; 
       } 

       testoNormalizzato = valore.ToString().Replace(",", ""); 
      } 
      else if ((valore.ToString().Length - valore.ToString().Replace(".", "").Length) > 1) 
      { 
       string[] pezzi = valore.ToString().Split('.'); 
       for (int i = 1; i < pezzi.Length; i++) 
       { 
        if (pezzi[i].Length != 3) 
         return false; 
       } 

       testoNormalizzato = valore.ToString().Replace(".", ""); 
      } 

      else 
       testoNormalizzato = valore.ToString(); 

     } 
     testoNormalizzato = testoNormalizzato.Replace(",", "."); 

     if (decimal.TryParse(testoNormalizzato, out n) && testoNormalizzato == Convert.ToDecimal(testoNormalizzato, new CultureInfo("en-US")).ToString().Replace(",", ".")) 
     { 
      valoreRestituito = Convert.ToDecimal(testoNormalizzato, new CultureInfo("en-US")); 
     } 
     return decimal.TryParse(testoNormalizzato, out n) && testoNormalizzato == Convert.ToDecimal(testoNormalizzato, new CultureInfo("en-US")).ToString().Replace(",", "."); 

    } 
隻言片語

第i個規範化數EN formatand接下來嘗試將其轉換

閱讀評論

最後一項測試是查看沒有文字與數字「相似」,因爲像「001」,「100 01」這樣的字符串不是數字。

比例是:每一個字符串必須保持inalterated:「001」轉換成爲數字「1」,即從原來的價值,因此必須避免

this is the fiddle

0

此皈依我建議你使用不同Regex用於驗證和像這樣的自定義解析方法:

public static decimal DecimalParse(string number) 
{ 
    if (new Regex(@"^\d+$").IsMatch(number)) 
    { 
     return decimal.Parse(number, CultureInfo.InvariantCulture); 
    } 
    if (new Regex(@"^(\d{0,3}(,\d{3})*(\.\d+)?)$").IsMatch(number)) 
    { 
     return decimal.Parse(number, CultureInfo.InvariantCulture); 
    } 
    return new Regex(@"^(\d{0,3}(\.\d{3})*(,\d+)?)$").IsMatch(number) 
     ? decimal.Parse(number.Replace(".", "").Replace(",", "."), CultureInfo.InvariantCulture) 
     : 0; 
} 

結果將是:

string num; 
num = "1,000";  Console.WriteLine("{0}", DecimalParse(num));  //1000 
num = ",01";  Console.WriteLine("{0}", DecimalParse(num));  //0.01 
num = ".02";  Console.WriteLine("{0}", DecimalParse(num));  //0.02 
num = "12,1";  Console.WriteLine("{0}", DecimalParse(num));  //12.1 
num = "12.1";  Console.WriteLine("{0}", DecimalParse(num));  //12.1 
num = "1.000,12"; Console.WriteLine("{0}", DecimalParse(num));  //1000.12 
num = "1.000.000,12"; Console.WriteLine("{0}", DecimalParse(num)); //1000000.12 
num = "1,000.12"; Console.WriteLine("{0}", DecimalParse(num));  //1000.12 
num = "1,000,000.12"; Console.WriteLine("{0}", DecimalParse(num)); //1000000.12 
num = "1000";  Console.WriteLine("{0}", DecimalParse(num));  //0 
num = "110.";  Console.WriteLine("{0}", DecimalParse(num));  //0 
num = "110,";  Console.WriteLine("{0}", DecimalParse(num));  //0 
num = "1.2.3";  Console.WriteLine("{0}", DecimalParse(num));  //0 
num = "1,2,3";  Console.WriteLine("{0}", DecimalParse(num));  //0 
+0

無法使用意大利文化在我的機器上工作:第一個正則表達式通過但是decimal.parge發生錯誤。你可以製作一個文化不可知論的版本嗎? –

+0

'返回decimal.Parse(數字,CultureInfo.InvariantCulture);'也出錯? –

+0

錯誤解決。但1,2被渲染爲12 –

相關問題