tags:

views:

1196

answers:

5

in C# , how can i check whether the value stored inside a string object( Ex : string strOrderId="435242A") is decimal or not?

+14  A: 

You can use Decimal.TryParse to check if the value can be converted to a Decimal type. You could also use Double.TryParse instead if you assign the result to a variable of type Double.

MSDN example:

value = "1,643.57";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
Meta-Knight
+2  A: 
decimal decValue;

if (decimal .TryParse(strOrderID, out decValue)
{ / *this is a decimal */ }
else
{ /* not a decimal */}
Jamie M
+5  A: 

Use the Decimal.TryParse function.

decimal value;
if(Decimal.TryParse(strOrderId, out value))
  // It's a decimal
else
  // No it's not.
Brandon
+2  A: 

you may try parsing it:

string value = "123";
decimal result;
if (decimal.TryParse(value, out result))
{
    // the value was decimal
    Console.WriteLine(result);
}
Darin Dimitrov
+1  A: 
    bool isDecimal;
    Decimal.TryParse(strOrderId,  out isDecimal);
if(isDecimal)
// strOrderId is decimal
else
// strOrderId is not a decimal
Jebli