About me

Michael L Perry

Improving Enterprises

Principal Consultant

@michaellperry

User login

Conditionals

There is a bug in the following code. Fix the bug and prove that it is correct.

public decimal GrowthPercent(decimal ThisQuarterSales, decimal LastQuarterSales)
{
    if (LastQuarterSales == 0.0m && ThisQuarterSales == 0.0m)
    {
        // No sales means no growth.
        return 0.0m;
    }
    if (LastQuarterSales == 0.0m && ThisQuarterSales > 0.0m)
    {
        // Any sales over zero is interpreted as 100% growth.
        return 100.0m;
    }
    return (ThisQuarterSales / LastQuarterSales - 1.0m) * 100.0m;
}

View the solution.