show/hide
fmt_dollar <- function(x, digits = 2) {
paste0("$", formatC(x, format = "f", digits = digits, big.mark = ","))
}Functions perform operations: calculate a total, build a table, create a graph. The function’s arguments are the inputs it needs to do its job and the output is what it gives back. For example, sum() takes a list of numbers as input and outputs their total.
In R, functions are defined with the function() keyword and assigned to a name. The body goes inside curly braces.
In Python, functions are defined with def, followed by a colon and an indented body. An explicit return statement is required; without it the function returns None.
Both languages let you create reusable functions for financial calculations. The key difference is that R automatically returns the last evaluated expression while Python requires return.
Before working through the math, it helps to have two small formatting utilities that handle the $ and % symbols seen throughout the book. They take a plain number and return a reader-friendly string.
fmt_dollar() adds a dollar sign, comma separators, and rounds to a set number of decimal places.
fmt_pct() expects a decimal rate (e.g. 0.07 for 7%) and converts it to a percentage string.
This can be used to format percentages as strings with a percent sign.
fmt_dollar() uses an f-string with , for thousands and .{digits}f for decimal places.
fmt_pct() takes a decimal rate, multiplies by 100, and formats the result.
Utility functions like fmt_pct() can be re-used in other, more complex functions.
In Excel, number formatting is applied through the toolbar. Select the cells to format and choose the appropriate option from the Number Format dropdown.
There are two options for money: Currency and Accounting.1
Both fmt_dollar() and fmt_pct() follow the same convention: input is always a plain number, output is always a string. They are used throughout the remaining sections to clean up results.
Fixed costs (rent, loans, insurance, utilities) should stay under ~60% of take-home pay.
Formula: Fixed-Cost Ratio = Fixed Costs ÷ Take-Home Pay × 100
The pipe operator (|>) passes the output of one function directly into the next function as its first argument, so the code reads left to right instead of inside out. We previously wrote the fmt_pct() function, so we can ‘pipe’ the output of fixed_cost_ratio() into fmt_pct() to get a formatted percentage string.
Read that as: compute the ratio, then format it as a percentage. The alternative without a pipe nests the calls and reads from the inside out, which is harder to follow.
When two steps always belong together, wrap them in a single function so callers only see one name.
This would also work:
Python has no native pipe operator. The equivalent is returning the result from fixed_cost_ratio() and passing this value to fmt_pct():
Python’s approach emphasizes explicit intermediate steps. Store the result of fixed_cost_ratio() in a variable (ratio), then pass it to fmt_pct(). This reads top-to-bottom and makes debugging easier since you can inspect intermediate values.
Both languages can call utility functions from within other functions; the key difference is that R’s pipe operator (|>) allows chaining without intermediate variables, while Python requires them or nested calls.
Read more about the pipe operator in R vs. Python.
Split every raise: invest half, spend half. The future value of contributions formula shows how powerful this habit is.
Formula: FV = PMT × [((1 + r)^n − 1) ÷ r]
Half of a $400/month raise, invested for 25 years at 7%:
Half of a $400/month raise, invested for 25 years at 7%:
Read more about the differences between Currency and Accounting formats in Excel.↩︎