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?