3

I have a Hashtable with results of parsing a certain JSON: decodedJson. decodedJson["key"] can be int, double, float, decimal or a string. I need to convert it to decimal if it's a number (which I plan to do with (decimal)decodedJson["key"]), or handle an error if it's not.

What's the most effective way to determine that?

Max Yankov
  • 12,551
  • 12
  • 67
  • 135

6 Answers6

6
if (decodedJson["key"] is decimal)
{
//do your action
}
Anarion
  • 2,406
  • 3
  • 28
  • 42
3

Since it can be any numeric type, you might want:

var i = decodedJson["key"];
bool isNumeric = i is byte || i is sbyte || i is short || i is ushort || 
                 i is int || i is uint || i is long || i is ulong || 
                 i is float || i is double || i is decimal;

if (isNumeric)
    Convert.ToDecimal(i);
else
    //handle

If you want to convert it to a type which is not the actual underlying type, a simple cast will not work. Convert class has the comprehensive tests it would need.


Or make it generic all the way if you want:

public static T To<T>(this object source) where T : IConvertible
{
    return (T)Convert.ChangeType(source, typeof(T));
}

public static bool IsNumeric(this Type t)
{
    return t.In(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), 
                typeof(int), typeof(uint), typeof(long), typeof(ulong), 
                typeof(float), typeof(double), typeof(decimal));
}

public static bool In<T>(this T source, params T[] list)
{
    return list.Contains(source);
}

Or find a more accurate version of IsNumeric here.

So now you call:

var i = decodedJson["key"]; 
if (i.GetType().IsNumeric())
    i.To<decimal>();
else
    //handle
Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
2

If the object is decimal you can do this

if (decodedJson["key"] is decimal)
{
   //Your code here
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

The is operator is probably your best choice

http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

    if(decodedJson["key"] is decimal){ 
// do something 
}
Austin
  • 754
  • 8
  • 12
0

Decimal.TryParse, documentation link below:

http://msdn.microsoft.com/en-us/library/system.decimal.tryparse(v=vs.110).aspx

Michael McGriff
  • 793
  • 10
  • 20
0

As per the question, if decodedJson["key"] can be anything from int, float, double, decimal or a string. We need to put a check for all types.

if(decodedJson["key"] is int ||  decodedJson["key"] is float || decodedJson["key"] is double || decodedJson["key"] is decimal)
{
     decimal convereted_value = (decimal) decodedJson["key"];
     // Rest of your code here 
}
else
{
     // Error handling code here.
}
  • 1
    This is again risky considering if the type is `int`, you cant cast to `decimal` type. `Convert` class is the best option. – nawfal Dec 26 '13 at 14:58
  • May be the Convert class is best option, but you can cast int to decimal, Check out this fiddle http://dotnetfiddle.net/khCkUt – NiralZaveri Dec 26 '13 at 15:09
  • Niral, of course that is possible. But you cant cast it when the value type is boxed. For instance, try: `object i = 0; decimal d = (decimal)i;` – nawfal Dec 26 '13 at 15:23