Tutorial: Tip

At this point, let's tackle a simple example, step-by-step, to get started. We are going to calculate the amount of the tip and the total bill given a subtotal amount and tip percentage. (Please note that this is a simplified example from the Tip template available in powerOne.)

The basic formulas we need to know:

      Tip Amount = Subtotal * Tip Percentage
      Total Bill = Subtotal - Tip Amount

As we discussed in the previous chapter on Advanced Templates, let us start by revisiting the basic structure of a row:

"Label" "Default" variable : equation ::

Label is what we see in the template to describe the row, default is its initial value, variable is its name and the equation is for calculating. The single colon tells us where the equation starts and the double-colon tells us where the row ends. (More details here.) Only the first three and the double-colon are required.

Labels, Defaults, Variables

I will start by first establishing the labels, default values and variables we will need:

      "Subtotal" "" subtotal ::
      "Tip%" "15" tip ::
      "Tip Amount" "" tip_amt ::
      "Total" "" total ::

If you try to save now you will get an error because powerOne is looking for something to calculate. The labels are anything you want. The best thing to do is make them descriptive enough that you don't need to look up the help documents to verify what is supposed to be entered.

Notice that most of the rows use "" for the default. That means there is not one. For Tip%, however, I used "15" since that is the generally accepted tip percentage (in the United States anyway.) We could have left that as "" also but adding a default makes it so you don't have to enter as much data and it helps the engine know when it can auto-calculate, again saving you a key press.

Finally, the variable is anything unique to define the row. I picked subtotal, tip, tip_amt and total but they could very well be a, b, c and d, too, as long as it starts with a letter and only uses letters, numbers and underscore. (The only restricted names are pi, i and e. Don't use these.)


Now that we have that set up, let's add in the equations for Tip Amount and Total:

      "Subtotal" "" subtotal ::
      "Tip%" "15" tip ::
      "Tip Amount" "" tip_amt : subtotal*tip% ::
      "Total" "" total : subtotal-tip% ::

I chose to use the percent math function to its fullest here as it is easier and faster than using subtotal - (subtotal *tip/100) to calculate Total.

There is a reason I didn't make Total equal to Subtotal less Tip Amount. powerOne will work correctly after a new template is requested but it won't re-calculate if you change the Tip% or Subtotal without resetting it first. The Tip Amount will be correct but Total will not. See the chapter on Auto Calculate, Chaining and Defaults for more details.

Advanced Topics

There is a lot more we can do to this such as set the number of decimal places, add separator rows and make output only rows as read-only. These are all discussed in later chapters.