Suppose the following method:
public static bool IsNumber<T>(string number) where T : struct ...
try
{
var numberConverted = (T) Convert.ChangeType(text, typeof(T));
return true;
}
catch (...
The idea is to call it like this:
var number = "$1,123.00";
var numberOk = IsNumber<decimal>(number);
The issue is that no matter what, I can't find a way to Convert.ChangeType
to accept numbers with the currency symbol (already tried passing in a IFormatProvider
with no success).
Is there a way to specify a NumberStyles
when using Convert.ChangeType
, or something different that would allow me to use the method IsNumber<T>
even if the string to check has a currency symbol?
Edit Jul-3-2016 9 PM UTC:
The idea is that this method verifies everything that the type T
requires. If I put a simple decimal.Parse
and include a NumberStyles.Currency
paramater, it will not validate that number
is an integer and should not have decimals, or that number
is a float
and the minimum and maximum value are different than the ones for decimal
. In other words, in a global and generic solution, I have no access to a Parse
-like method or something that accepts a NumberStyles
parameter.
This edit is to explain why I think that this is no duplicate of the question Problem parsing currency text to decimal type