19

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

Shyju
  • 214,206
  • 104
  • 411
  • 497

9 Answers9

37

Use the Decimal.TryParse function.

decimal value;
if(Decimal.TryParse(strOrderId, out value))
  // It's a decimal
else
  // No it's not.
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • 3
    This will only work if any number can be considered a Decimal. If you need to distinguish between numeric types, it'll consider integral types as decimals as well. – Remi Despres-Smyth Apr 17 '12 at 12:56
  • You should consider decimal format and current culture. For example, correct decimal value for en-Us 643.57 doesn't parsing in ru-RU culture by this method. – Sergey Popov Mar 05 '14 at 15:41
  • 1
    I know this is old, but I add some extra validations and also check for a decimal comma, witch in my case is the decimal separator. `if (!Decimal.TryParse(strOrderId, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out decimalNumber) || strOrderId.IndexOf(",") > -1) { // not Decimal } else { // decimal }` – Pimenta Aug 23 '17 at 10:44
30

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:

string value = "1,643.57";
decimal number;
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
5
decimal decValue;

if (decimal.TryParse(strOrderID, out decValue)
{ / *this is a decimal */ }
else
{ /* not a decimal */}
Jamie M
  • 870
  • 1
  • 8
  • 18
5

Take advantage of the discard symbol _ and use

if (decimal.TryParse(stringValue, out _))
{
   // valid decimal 
}
else
{
   // not decimal
}
4

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,023,142
  • 271
  • 3,287
  • 2,928
2

In case if we do not want use extra variable.

string strOrderId = "435242A";

bool isDecimal = isDecimal(strOrderId);


public bool isDecimal(string value) {

  try {
    Decimal.Parse(value);
    return true;
  } catch {
    return false;
  }
}
1

This simple code will allow integer or decimal value and rejects alphabets and symbols.

      foreach (char ch in strOrderId)
        {
            if (!char.IsDigit(ch) && ch != '.')
            {

              MessageBox.Show("This is not a decimal \n");
              return;
            }
           else
           {
           //this is a decimal value
           }

        }
anandd360
  • 298
  • 3
  • 14
1

Think simple.

decimal decNumber = decimal.Parse("9.99");
if (decNumber % 1 > 0)
{
   //decimal area
}
else
{
   //int area
}
Umut D.
  • 1,746
  • 23
  • 24
0

Declare decimal out value in TryParse

if(Decimal.TryParse(stringValue,out decimal dec))
{
    // ....
}
MindSwipe
  • 7,193
  • 24
  • 47
Dale Kilian
  • 71
  • 1
  • 3