New answer (to new question)
Okay, so you've got a value of 579.99722222222222222222222222
- and you're asking that to be rounded to two decimal places. Isn't 580.00 the natural answer? It's closer to the original value than 579.99 is. It sounds like you essentially want flooring behaviour, but with a given number of digits. For that, you can use:
var floored = Math.Floor(original * 100) / 100;
In this case, you can do both in one step:
var hours = Math.Floor(dTotal / 36) / 100;
... which is equivalent to
var hours = Math.Floor((dTotal / 3600) * 100) / 100;
Original answer (to original question)
Sounds like you've probably got payTotal
in an inappropriate form to start with:
using System;
class Test
{
static void Main()
{
decimal pay = 2087975.7m;
decimal time = pay / 3600;
Console.WriteLine(time); // Prints 579.99325
}
}
This is the problem:
var payTotal = 2087975.7;
That's assigning payTotal
to a double
variable. The value you've actually got is 2087975.69999999995343387126922607421875, which isn't what you wanted. Any time you find yourself casting from double
to decimal
or vice versa, you should be worried: chances are you've used the wrong type somewhere. Currency values should absolutely be stored in decimal
rather than double
(and there are various other Stack Overflow questions talking about when to use which).
See my two articles on floating point for more info:
(Once you've got correct results, formatting them is a different matter of course, but that shouldn't be too bad...)