Skip to main content

#137 - Tip Calculator

Create a function that given a total bill, and an integer percentage value between 0 and 100, computes the tip at the specified percentage. You can assume the percentage specified will be an integer within the valid range.

For an example, computeTip(200.0, 50) evaluates to 100.0, because 50% of 200 is 100.

 

public Decimal computeTip(Decimal total, Integer percent) {

    Decimal Tip;

    Tip = (total * percent) / 100;

    return tip;

}

2 comments
  1. Sep 12, 9:44 PM

    The calculation itself looks correct. If total is 200 and percent is 50, then:

    (200 * 50) / 100 = 100

    So I would simplify the method a little:

    public Decimal computeTip(Decimal total, Integer percent) {

    Decimal tip = (total * percent) / 100;

    return tip;

    }

    One small thing to watch is the variable name. You declare Decimal Tip; but return tip;. Depending on the language, capitalization can matter, so keeping it consistently lowercase avoids that issue.

    You could also calculate the percentage separately as (total * percent) / 100, which is the general approach for converting an integer percentage into an amount.

    For example, 15% of 80 would be 12.00.

0/9000