Search code examples
c#decimal-point

C# math round on 0.8 not on .5


Suppose I have number 87.6 of type double here I want to round it, so I have applied C# build in method of round to get the output something like this

  double test2 = 87.6;
  Console.WriteLine(Math.Round(test2, 0));

this will generate 88 which is fine. However, I wanted to be round back to 87 my logic would be on 0.8 and not on 0.5. So for instance if my input is 87.8 then I want to get 88 and if my input is 88.7 then I want to round it to 87.


Solution

  • I've got the answer from the comment section here is the logic for this

    double test2 = 87.6;
    test2 -= 0.3;
    Console.WriteLine(Math.Round(test2, 0));
    

    This 0.3 will make the difference