Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:04:59+00:00 2026-06-16T17:04:59+00:00

Is there a single-line way of casting an object to a decimal? data type?

  • 0

Is there a single-line way of casting an object to a decimal? data type?

My code looks something like this:

foreach(DataRow row in dt.Rows)
{
    var a = new ClassA()
    {
         PropertyA = row["ValueA"] as decimal?,   
         PropertyB = row["ValueB"] as decimal?,
         PropertyC = row["ValueC"] as decimal?
    };

    // Do something

}

However casting an object to a decimal? doesn’t work the way I expect it to, and returns null every time.

The data rows are read from an Excel file, so the data type of the object in each row is either a double if there is a value, or a null string if it’s left blank.

The suggested method to perform this cast is to use decimal.TryParse, however I do not want to create a temp variable for every decimal property on the class (I have about 7 properties on my actual class that are decimals, not 3).

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

Is there a way I can cast a potentially null value to a decimal? in a single line?

I tried the answer posted here, however it doesn’t appear to be working and gives me a null value as well because row["ValueA"] as string returns null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:05:00+00:00Added an answer on June 16, 2026 at 5:05 pm

    providing there is a value it reads as a double, otherwise I think it’s a string

    Okay, so this complicates matters as sometimes you need to parse it and sometimes you don’t.

    The first thing we’ll want to do is check if it’s already a double and cast it, because casting is much cheaper than parsing; parsing is expensive.

    Given that this logic will be non-trivial, it belongs in it’s own method:

    public static deciaml? getDecimal(object rawValue)
    {
        decimal finalValue;
    
        double? doubleValue = rawValue as double?;
        if(doubleValue.HasValue)
            return (decimal) doubleValue.Value;
        else if(decimal.TryParse(rawValue as string, out finalValue))
        {
            return finalValue;
        }
        else
        {
            return null;//could also throw an exception if you wanted.
        }
    }
    

    If you know that it will always be a double, and you’ll never want the string values, then it’s easier still. In that case you need to cast it to a double, first, since that’s it’s real type, and then you can easily convert it to a decimal:

    PropertyA = (decimal?)(row["ValueA"] as double?);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a possible way to rewrite this in a single line through the
Is there any way to do this in a single line: TextView tv =
Is there a way to configure Sonar to ignore single-line if-statements like if (value
Is there a way to comment out a single line in HTML using just
Is there a way in python to turn a try/except into a single line?
Is there a decent way to declare a long single line string in C#,
Is there a way, in Python, to modify a single line in a file
Is there any way to CLS a single line of output? I don't believe
Is there a way to set a breakpoint at every line in the code
I have seen people put all their JS code into one single line. (something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.