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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:58:49+00:00 2026-05-12T18:58:49+00:00

Summary of the problem: For some decimal values, when we convert the type from

  • 0

Summary of the problem:

For some decimal values, when we convert the type from decimal to double, a small fraction is added to the result.

What makes it worse, is that there can be two “equal” decimal values that result in different double values when converted.

Code sample:

decimal dcm = 8224055000.0000000000m;  // dcm = 8224055000
double dbl = Convert.ToDouble(dcm);    // dbl = 8224055000.000001

decimal dcm2 = Convert.ToDecimal(dbl); // dcm2 = 8224055000
double dbl2 = Convert.ToDouble(dcm2);  // dbl2 = 8224055000.0

decimal deltaDcm = dcm2 - dcm;         // deltaDcm = 0
double deltaDbl = dbl2 - dbl;          // deltaDbl = -0.00000095367431640625

Look at the results in the comments. Results are copied from debugger’s watch.
The numbers that produce this effect have far less decimal digits than the limit of the data types, so it can’t be an overflow (I guess!).

What makes it much more interesting is that there can be two equal decimal values (in the code sample above, see “dcm” and “dcm2”, with “deltaDcm” equal to zero) resulting in different double values when converted. (In the code, “dbl” and “dbl2”, which have a non-zero “deltaDbl”)

I guess it should be something related to difference in the bitwise representation of the numbers in the two data types, but can’t figure out what! And I need to know what to do to make the conversion the way I need it to be. (like dcm2 -> dbl2)

  • 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-12T18:58:50+00:00Added an answer on May 12, 2026 at 6:58 pm

    Interesting – although I generally don’t trust normal ways of writing out floating point values when you’re interested in the exact results.

    Here’s a slightly simpler demonstration, using DoubleConverter.cs which I’ve used a few times before.

    using System;
    
    class Test
    {
        static void Main()
        {
            decimal dcm1 = 8224055000.0000000000m;
            decimal dcm2 = 8224055000m;
            double dbl1 = (double) dcm1;
            double dbl2 = (double) dcm2;
    
            Console.WriteLine(DoubleConverter.ToExactString(dbl1));
            Console.WriteLine(DoubleConverter.ToExactString(dbl2));
        }
    }
    

    Results:

    8224055000.00000095367431640625
    8224055000
    

    Now the question is why the original value (8224055000.0000000000) which is an integer – and exactly representable as a double – ends up with extra data in. I strongly suspect it’s due to quirks in the algorithm used to convert from decimal to double, but it’s unfortunate.

    It also violates section 6.2.1 of the C# spec:

    For a conversion from decimal to float or double, the decimal value is rounded to the
    nearest double or float value. While this conversion may lose precision, it never causes
    an exception to be thrown.

    The “nearest double value” is clearly just 8224055000… so this is a bug IMO. It’s not one I’d expect to get fixed any time soon though. (It gives the same results in .NET 4.0b1 by the way.)

    To avoid the bug, you probably want to normalize the decimal value first, effectively “removing” the extra 0s after the decimal point. This is somewhat tricky as it involves 96-bit integer arithmetic – the .NET 4.0 BigInteger class may well make it easier, but that may not be an option for you.

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

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As others have stated, you can use the DesignMode property… May 12, 2026 at 10:05 pm
  • Editorial Team
    Editorial Team added an answer Try something like this SELECT tableA.num FROM tableA UNION ALL… May 12, 2026 at 10:05 pm
  • Editorial Team
    Editorial Team added an answer Seems like fine design to me. I'm not sure about… May 12, 2026 at 10:05 pm

Related Questions

I installed the latest version of Wordpress and added the WP-Syntax plugin (and also
Today I had a problem with my old Eclipse 3.4 installation and I had
I am reviewing a database model. I need some help with a part of
I'm using jQuery to setup an Ajax request that grabs an XML feed from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.