c# - How to check if column can be converted to double? (DataTable.Column.DataType) -
i need simple condition replace (incomplete) if
condition.
// dont want write possible data types if (col.datatype == typeof(int) || col.datatype == typeof(int64) ... types) { // want on numeric columns // (convert numbers double datatype) } else { // string , other non-numbers remain unchanged }
i trying this:
col.datatype.isnumeric()
but there no such method in class.
i cant use tryparse()
method on data because there data.
condition must determined datatable column datatype property.
is there simple method simplify if
?
you use datatype did, or make function (see determine if datacolumn numeric):
public static bool isnumeric(this datacolumn col) { if (col == null) return false; // numeric types var numerictypes = new [] { typeof(byte), typeof(decimal), typeof(double), typeof(int16), typeof(int32), typeof(int64), typeof(sbyte), typeof(single), typeof(uint16), typeof(uint32), typeof(uint64)}; return numerictypes.contains(col.datatype); }
and use
if (col.isnumeric()) { // column numeric }
now, content of column, try using double.tryparse()
method, this:
double temp; if (double.tryparse(col["column"].tostring(), out temp)) { // content can converter , can read temp variable. }