2

I need to adjust the value of a double variable. In order to do that I've written this extension method

public static class Helper
{
    public static double Adjust(this double value, double l, double h)
    {
        while (value < l)
            value += h;
        while (value >= h)
            value -= h;

        return value;
    }
}

But it doesn't give me the expected result.

Here I am calling my code:

private void Form1_Load(object sender, EventArgs e)
{
    double a = 10.1;
    Text = a.Adjust(5,10).ToString();
}

I was expecting the result to be 0.1 but is 0.099999999999999646. I have figured out that it have something to do with the precision of the double datatype. But how can I get my result to be 0.1?

Scott Newson
  • 2,745
  • 2
  • 24
  • 26
Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • 1
    Use _decimal_ for this operation. – Gustav Jan 09 '16 at 19:04
  • Are you ***sure*** that *double* is a more appropriate datatype than, for example, *decimal(18,2)* or *decimal(18,4)*? Appropriate selection of datatype is part of what distinguishes senior developers from intermediates and juniors. Generally the *double* and *single* datatypes are only appropriate for scientific, engineering, and economic analysis; not routine accounting. – Pieter Geerkens Jan 09 '16 at 19:05
  • 1
    Worth to read http://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net?lq=1 – Steve Jan 09 '16 at 19:08

2 Answers2

3

You are correct, the double datatype isn't exact for mathematical operations.

Try using an exact decimal datatype like System.Decimal

https://msdn.microsoft.com/en-us/library/system.decimal.aspx

Scott Newson
  • 2,745
  • 2
  • 24
  • 26
2

The result occurs due to rounding numbers represented in the float or double data type. The reason for the unexprected behaviour is the fact that not every rational number can be represented within a specific floating point type and the 'resolution' of representable numbers decreases with increasing absolute value.

Codor
  • 17,447
  • 9
  • 29
  • 56