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

  • Home
  • SEARCH
  • 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 8445793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:45:04+00:00 2026-06-10T09:45:04+00:00

I stumbled across a method in my code where a rounded value is calculated

  • 0

I stumbled across a method in my code where a rounded value is calculated wrong in my code.
I am aware about the problem with comparing double values generated unexpected results.

Example

    double x = 19.08;
    double y = 2.01;
    double result = 21.09;

    if (x + y == result)
    {
        // this is never reached
    }

Explanation here: http://csharpindepth.com/Articles/General/FloatingPoint.aspx

However, until now, I expected the Math.Round() method to be accurate even with double values.

Look at this code.

        var decimals = 2;
        var value1 = 4.725;
        var value2 = 4.725M;

        var result1 = Math.Round(value1, decimals, MidpointRounding.ToEven);
        var result2 = Math.Round(value1, decimals, MidpointRounding.AwayFromZero);
        var result3 = Math.Round(value2, decimals, MidpointRounding.ToEven);
        var result4 = Math.Round(value2, decimals, MidpointRounding.AwayFromZero);

        Console.WriteLine("Double (ToEven): {0}", result1); // outputs 4.72
        Console.WriteLine("Double (AwayFromZero): {0}", result2); // outputs 4.72 (expected: 4.73)
        Console.WriteLine("Decimal (ToEven): {0}", result3); // outputs 4.72
        Console.WriteLine("Decimal (AwayFromZero): {0}", result4); // outputs 4.73

For me, it is totally clear that result2 should be 4.73. However, it is not the case.
Can someone explain why?

  • 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-10T09:45:06+00:00Added an answer on June 10, 2026 at 9:45 am

    Well, you may want to rethink your notion of »totally clear« because 4.725 (as opposed to 4.625) cannot be represented exactly with a double. It’s actually exactly

    4.7249999999999996447286321199499070644378662109375

    Keep in mind that floating-point numbers are just an approximation to the mathematical concept of real numbers – many of your intuitive notions about how numbers should behave don’t apply. You end up with a value that is approximately 4.725 but obviously just slightly below it. The midpoint rounding mode will therefore do nothing here as it’s not exactly halfway between two possible numbers to round.

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

Sidebar

Related Questions

I stumbled across a little problem with an ASP.NET Web Application. I got a
I stumbled across this problem in F#. Suppose, I want to declare two types
I stumbled across this C code today. Can anyone tell me what the 'where'
While reading a book about JavaScript I stumbled across an example: var names =
about a year ago I stumbled across a nice feature in Java that I
I'm trying to truncate some text in PHP and have stumbled across this method
I stumbled across some code and I was curious as to possible reasons why
I just stumbled across this bug in some legacy code: class MyAPIHandler { private:
I stumbled across some code similar to below: private void SomeCallBack(object state) { lock
I recently stumbled across this entry in the google testing blog about guidelines for

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.