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 5932529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:50:48+00:00 2026-05-22T14:50:48+00:00

I need to convert between various types (decimal, int32, int64, etc.) but I want

  • 0

I need to convert between various types (decimal, int32, int64, etc.) but I want to make sure I am not loosing any data. I’ve found that normal Convert methods (including casting) will truncate data without warning.

decimal d = 1.5;
int i = (int)d;
// i == 1

I would like if there was a convert or TryConvert method that would throw or return false if a conversion was dropping data. How can I accomplish this?

If possible, I would like to do this in a generic sense, so I can do it all given two Type objects, and an object instance (where runtime type is convertFrom type). Like this:

object ConvertExact(object convertFromValue, Type convertToType)
{
    if ( ** conversion not possible, or lossy ** )
        throw new InvalidCastException();

    // return converted object
}

Similar to this question, but here the number is truncated.

  • 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-05-22T14:50:49+00:00Added an answer on May 22, 2026 at 2:50 pm

    How about this:

    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ConvertExact(2.0, typeof(int)));
            Console.WriteLine(ConvertExact(2.5, typeof(int)));
        }
    
        static object ConvertExact(object convertFromValue, Type convertToType)
        {
            object candidate = Convert.ChangeType(convertFromValue,
                                                  convertToType);
            object reverse =  Convert.ChangeType(candidate,
                                                 convertFromValue.GetType());
    
            if (!convertFromValue.Equals(reverse))
            {
                throw new InvalidCastException();
            }
            return candidate;
        }
    }
    

    Note that this isn’t perfect – it will happily convert both 2.000m and 2.00m to 2 for example, despite the fact that that does lose information (the precision). It’s not losing any magnitude though, and this may be good enough for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert between these two classes, and want to maintain DRY but
I need to convert between python objects and c strings of various encodings. Going
I need a cross-platform library/algorithm that will convert between 32-bit and 16-bit floating point
I need to convert time between timezones in C (on linux, so anything specific
I need to convert a number between 1 and 6000000 to a letter combination
I need to convert between wstring and string. I figured out, that using codecvt
I got a number between 0-255 and need to convert it to a RGB
I have a C# app and I need to convert between 3 different units
I need to convert latitude/longitude coordinates into Easting/Northing coordinates in the Alberta 10 TM
I need to convert a named instance of SQL server 2005, to a default

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.