Int32 intValue;
if (Int32.TryParse(mystring, out intValue)){
// mystring is an integer
}
または、10進数の場合:
Double dblValue;
if (Double.TryParse(mystring, out dblValue)){
// mystring has a decimal number
}
いくつかの例、ところで、ここで見つけることができます。
Testing foo:
Testing 123:
It's an integer! (123)
It's a decimal! (123.00)
Testing 1.23:
It's a decimal! (1.23)
Testing $1.23:
It's a decimal! (1.23)
Testing 1,234:
It's a decimal! (1234.00)
Testing 1,234.56:
It's a decimal! (1234.56)
私がテストしたもう2つの:
Testing $ 1,234: // Note that the space makes it fail
Testing $1,234:
It's a decimal! (1234.00)
Testing $1,234.56:
It's a decimal! (1234.56)
Testing -1,234:
It's a decimal! (-1234.00)
Testing -123:
It's an integer! (-123)
It's a decimal! (-123.00)
Testing $-1,234: // negative currency also fails
Testing $-1,234.56: