0

I have the following function that typically need to return a value. I calculate the value in a calculator app and I get values just fine. But in the program below the value variable value is always 0. I have tried using long, double, float but nothing works. Please help.

 public string CalculateElapsedPercent(DateTime endTime, DateTime startTime)
        {
            string result = string.Empty;
            DateTime currentTime = DateTime.Now;
            if (currentTime > endTime)
            {
                result = " (100 %)";
                return result;
            }
            long nr = (currentTime - startTime).Ticks;
            long dr = (endTime - startTime).Ticks;
            double value = (nr / dr) * 100.0;
            result = " (" + value.ToString() + " %)";
            return result;
        }
nikhil
  • 1,578
  • 3
  • 23
  • 52

5 Answers5

1

Since both nr and dr are long, the result of (nr / dr) is long, and since dr is greater that nr, the result is equal to 0.

In order to fix that, you can convert it to double during calculation:

double value = ((double)nr / (double)dr) * 100.0;

Or you can define nr and dr as doubles:

double nr = (currentTime - startTime).Ticks;
double dr = (endTime - startTime).Ticks;
double value = (nr / dr) * 100.0;

The behavior in your original code sample is called integer division. See Division operator article in C# Specification or Why integer division in c# returns an integer but not a float? stackoverflow question for more info

Community
  • 1
  • 1
Kel
  • 7,680
  • 3
  • 29
  • 39
1

nr and dr are integers, which means the nr/dr is an integer division, and since dr will always be larger than nr, you get zero.

Convert them to a double before dividing:

double nr = Convert.ToDouble((currentTime - startTime).Ticks);
double dr = Convert.ToDouble((endTime - startTime).Ticks);
double value = (nr / dr) * 100.0;
smead
  • 1,768
  • 15
  • 23
1

You are performing integer division (nr / dr), which means you lose all the decimal values.

You instead want to perform floating point division. You can do that by casting nr or dr to double. Or, in your case, you could just move up the multiplication by 100.0 a little (multiplying by 100.0, forces nr to be cast to a double, which then means that dr will also be automatically cast to double for the division):

double value = nr * 100.0 / dr;
sstan
  • 35,425
  • 6
  • 48
  • 66
0

nr and dr are both long which is essentially integers. If you go with a (nr / dr) and dr is larger than nr, you would end up with zero as a result of the division. Just cast nr and dr to double and your problems would be solved. :)

    public string CalculateElapsedPercent(DateTime endTime, DateTime startTime)
    {
        string result = string.Empty;
        DateTime currentTime = DateTime.Now;
        if (currentTime > endTime)
        {
            result = " (100 %)";
            return result;
        }
        long nr = (currentTime - startTime).Ticks;
        long dr = (endTime - startTime).Ticks;
        double value = ((double)nr / (double)dr) * 100.0;
        result = " (" + value.ToString() + " %)";
        return result;
    }
Swagata Prateek
  • 1,076
  • 7
  • 15
0

Others have already explained the issue, but I would also like to suggest a little improvement to your method. For example, like this:

public static string CalculateElapsedPercent(DateTime endTime, DateTime startTime)
{
    double ratio;
    DateTime currentTime = DateTime.Now;
    if (currentTime > endTime)
    {
        ratio = 1;
    }
    else
    {
        var elapsed = currentTime - startTime;
        var total = endTime - startTime;
        ratio = elapsed.TotalMinutes / total.TotalMinutes;
    }
    return string.Format(" ({0:P2})", ratio);
}
Andrew
  • 7,602
  • 2
  • 34
  • 42