Cast object to decimal? (nullable decimal)

93,807

Solution 1

Unboxing only works if the type is identical! You can't unbox an object that does not contain the target value. What you need is something along the lines of

decimal tmpvalue;
decimal? result = decimal.TryParse((string)value, out tmpvalue) ?
                  tmpvalue : (decimal?)null;

This looks whether the value is parsable as a decimal. If yes, then assign it to result; else assign null. The following code does approximately the same and might be easier to understand for people not familiar with the conditional operator ?::

decimal tmpvalue;
decimal? result = null;
if (decimal.TryParse((string)value, out tmpvalue))
    result = tmpvalue;

Solution 2

you should parse the decimal. But if you want your decimal to be null when the string is not correct, use TryParse :

decimal parsedValue;
decimal? temp = decimal.TryParse(value, out parsedValue)
                ? value
                : (decimal?)null;

This way you will avoid exceptions while parsing ill formated strings.

Almost all primitive types provide a Parse and TryParse methods to convert from string.

Is is also recommended to pass a culture for the provider argument to the method to avoid problems with the decimal separator. If you're reading from another system, CultureInfo.InvariantCulture is probably the way to go (but it's not the default).

bool TryParse(string s, NumberStyles style,
  IFormatProvider provider, out decimal result)

Solution 3

If you do not want to parse strings, but want to ensure that you receive either null, a decimal or a nullable decimal, then you could do something like this:

public static Nullable<T> Convert<T>(object input) 
    where T : struct
{
    if (input == null)
        return null;
    if (input is Nullable<T> || input is T)
        return (Nullable<T>)input;
    throw new InvalidCastException();
}

You could make it return null on the last line instead if you wished to avoid exceptions, although this would not distinguish between real null values and bad casts.

Note that you have to use the "is" operator, as the "as" operator does not work on value types, and casting without checking may thrown an InvalidCastException.

You could also make it an extension method:

public static class ObjectExtensions
{
    public static Nullable<T> ToNullable<T>(this object input)
        where T : struct
    {
        if (input == null)
            return null;
        if (input is Nullable<T> || input is T)
            return (Nullable<T>)input;
        throw new InvalidCastException();
    }
}

And use it like this:

object value = 123.45m;
decimal? dec = value.ToNullable<decimal>();

This will help avoid code contract warnings about unboxing null references.

Solution 4

and if you use decimal? temp = (decimal?)value;

Solution 5

Surprisingly, but good old System.Convert.ToDecimal(myNullableDoubleBoxedInObject) works perfectly:

decimal? myNullableDecimal = 0.15m;
object myNullableDoubleBoxedInObject = myNullableDouble ;
decimal myDecimal = System.Convert.ToDouble(myNullableDoubleBoxedInObject);
Share:
93,807
Natrium
Author by

Natrium

SOreadytohelp

Updated on August 12, 2020

Comments

  • Natrium
    Natrium over 3 years

    If have this in the setter of a property:

    decimal? temp = value as decimal?;
    

    value = "90"

    But after the cast, temp is null...

    What is the proper way to do this cast?

  • Natrium
    Natrium about 15 years
    that doesn't work. I can't explain why. In debugmode, Visual Studio wants to show the disassembly...
  • thinkbeforecoding
    thinkbeforecoding about 15 years
    I had forgotten the (decimal?) cast for null... sorry, but the culture part is still important.
  • Jon Skeet
    Jon Skeet about 15 years
    I wouldn't use the word "identical" here. For instance, you can unbox between enum types and their underlying type, T and T?, and some other odd cases IIRC. The CLR is more permissive than one might expect.
  • Jon Skeet
    Jon Skeet about 15 years
    (But yes, you shouldn't expect unboxing to parse a string :)
  • Konrad Rudolph
    Konrad Rudolph about 15 years
    @Jon: have you got a better formulation? Lacking that, I'm going to copy your comment into my answer because it expresses the caveat nicely.
  • thinkbeforecoding
    thinkbeforecoding about 15 years
    Actually, Parse with one argument is a shortcut for TryParse with style and culture with default arguments.. There is no one to one mapping between string and primitive types, so there's no cast.
  • thinkbeforecoding
    thinkbeforecoding about 15 years
    The case of enums is the only one where you can unbox to another type. Other unboxing simply get the object'ed value and assign it to the variable on the stack. No other conversion occures.
  • offner
    offner over 14 years
    This answer is incorrect as assigning 'value' to 'temp' will not work. It should be ? parsedValue : (decimal?)null;
  • pauloya
    pauloya over 12 years
    It won't work if value holds an instance of string. It should work if it holds an instance of decimal.
  • Shiva
    Shiva about 7 years
    Have you tested your extension method?? I tried it and got a NullReferenceException for a valid double value that needed to be converted to nullable decimal
  • Stephen Drew
    Stephen Drew over 6 years
    @Shiva The extension method is not intended for doubles, as it states in the first line: "that you receive either null, a decimal or a nullable decimal"