2010-01-19 20 views

回答

10
double n; 
if (Double.TryParse("128337.812738", out n)) { 
    // ok 
} 

作品假設數量不溢出雙

一個巨大的字符串,嘗試正則表達式:

if (Regex.Match(str, @"^[0-9]+(\.[0-9]+)?$")) { 
    // ok 
} 

添加在科學記數法(E/E)或+/-跡象,如果需要...

+0

應該用什麼參數? 它會接受一個巨大的字符串?有什麼限制嗎? – Maneesh 2010-01-19 09:41:20

+0

對於一個巨大的字符串,你需要一個正則表達式 – jspcal 2010-01-19 09:43:07

+0

感謝reg Exp解決方案!謝謝jspcal – Maneesh 2010-01-19 10:07:24

0

我認爲你可以使用正則表達式,在正則表達式類

Regex.IsMatch(yourStr,「\ d」)

或類似的東西在我頭頂。

或者你可以使用Parse方法int.Parse(...)

-1

這應該工作:

bool isNum = Integer.TryParse(Str, out Num); 
+0

Int.TryParse將在他的第三個「123.67」測試用例上失敗 – ZombieSheep 2010-01-19 09:43:50

3

您可以使用double.TryParse

string value; 
double number; 

if (Double.TryParse(value, out number)) 
    Console.WriteLine("valid"); 
else 
    Console.WriteLine("invalid"); 
2

MSDN採取的措施(如何通過使用Visual C#實現Visual Basic .NET IsNumeric功能):

// IsNumeric Function 
static bool IsNumeric(object Expression) 
{ 
    // Variable to collect the Return value of the TryParse method. 
    bool isNum; 

    // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero. 
    double retNum; 

    // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent. 
    // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned. 
    isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum); 
    return isNum; 
} 
2

這應該工作,不管字符串有多長:

string s = "12345"; 
bool iAllNumbers = s.ToCharArray().All (ch => Char.IsDigit (ch) || ch == '.'); 
+0

這也會匹配「123.456.789」,這不是有效的號碼 – 2010-01-19 09:48:19

+0

@Philippe:你是對的。我認爲RegExp是最乾淨的方式。 – Tarydon 2010-01-19 09:49:40

+0

@Tarydon System.Array不包含All?的定義! – Alex 2016-07-16 09:30:38

2

使用正則表達式是最簡單的方法(但不是最快的):

bool isNumeric = Regex.IsMatch(s,@"^(\+|-)?\d+(\.\d+)?$"); 
+0

你錯過了'.23'的情況。 – leppie 2010-01-19 09:56:52

+0

可能的話,如果你想允許25年前由Commodore 64計算機使用的號碼:-) – 2010-01-19 09:58:49

+0

抱歉的哥們,但C#也接受它,Scheme也是如此,並且大多數語言都帶有IEEE浮點數:) – leppie 2010-01-19 12:37:52

0

如果您收到的字符串作爲一個參數更靈活的方式是使用正則表達式,如其他文章中所述。 如果您從用戶那裏獲得輸入,您可以掛鉤KeyDown事件並忽略所有不是數字的鍵。這樣你就可以確定你只有數字。

1

如前所述上面如果你不喜歡這樣(出於某種原因),您可以使用double.tryParse

,您可以編寫自己的擴展方法:

public static class ExtensionMethods 
    { 
     public static bool isNumeric (this string str) 
     { 
      for (int i = 0; i < str.Length; i++) 
      { 
       if ((str[i] == '.') || (str[i] == ',')) continue; //Decide what is valid, decimal point or decimal coma 
       if ((str[i] < '0') || (str[i] > '9')) return false; 
      } 

      return true; 
     } 
    } 

用法:

string mystring = "123456abcd123"; 

if (mystring.isNumeric()) MessageBox.Show("The input string is a number."); 
else MessageBox.Show("The input string is not a number."); 

輸入:

1234 56abcd123

123。6

輸出:

真正

相關問題