Start learning free

LESSON 190 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Go: Discount and Tax with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

item := "Coffee Mug"
price := 12
quantity := 3
taxRate := 8
discount := 5
_ = item

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal := price * quantity.
  • afterDiscount := subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = afterDiscount * taxRate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total := afterDiscount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with fmt.Println(result).

Compute the four numbers (subtotal, afterDiscount, tax, total) before formatting.

Use strconv.Itoa(...) to convert each number to text before concatenating it.

Go's int / int truncates, so afterDiscount * taxRate / 100 already rounds down.

item := "Coffee Mug"
price := 12
quantity := 3
taxRate := 8
discount := 5
_ = item

subtotal := price * quantity
afterDiscount := subtotal - discount
tax := afterDiscount * taxRate / 100
total := afterDiscount + tax
line1 := "Subtotal: $" + strconv.Itoa(subtotal)
line2 := "Discount: $" + strconv.Itoa(discount)
line3 := "Tax: $" + strconv.Itoa(tax)
line4 := "Total: $" + strconv.Itoa(total)
result := line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
fmt.Println(result)

STEP 1 OF 10
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Go, explained

    Problem overview

    This version of the receipt adds a flat discount and a percentage sales tax to a one-item purchase. The starter has item, price, quantity, taxRate := 8 (a percentage) and discount := 5 (a flat amount). The program prints four lines, Subtotal, Discount, Tax and Total:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33
    

    The rules, in order: the subtotal is the price times the quantity; the discount is taken off the subtotal first; the tax is charged on the discounted amount; the total is the discounted amount plus the tax. All values are integers, so the tax is rounded down.

    Approach: one named value per rule

    Each rule becomes a variable: subtotal, afterDiscount, tax and total. Then each output row becomes a string variable, line1 to line4, and the rows are joined with newlines into result.

    The order of the arithmetic matters. The discount comes off before the tax, so the tax is computed on afterDiscount, not on subtotal. With the starting data that is 31, and 31 * 8 / 100 is 2 after the fraction is cut off.

    The percentage uses integer math. afterDiscount * taxRate / 100 multiplies first and divides by 100 last. Dividing two int values in Go yields an int, which drops the fraction, so no floating-point numbers are needed.

    It helps to read the starting data through the chain: 12 * 3 is 36, minus 5 is 31, 31 * 8 / 100 is 2, and 31 + 2 is 33. Each result feeds the next line, so the variables must be created in this order.

    The item name is never printed in this version. That is why the starter data keeps it alive with a blank assignment.

    Step by step

    1. subtotal is price * quantity: the cost before the discount and the tax.
    2. afterDiscount is subtotal - discount.
    3. tax is afterDiscount * taxRate / 100.
    4. total is afterDiscount + tax: the amount the customer pays.
    5. line1 is "Subtotal: $" followed by strconv.Itoa(subtotal).
    6. line2 is the Discount row, built the same way from discount.
    7. line3 is the Tax row, from tax.
    8. line4 is the Total row, from total.
    9. result joins the four rows with "\n" between each two. Four rows need three newlines.
    10. Print result with fmt.Println.

    The solution in Go

    item := "Coffee Mug"
    price := 12
    quantity := 3
    taxRate := 8
    discount := 5
    _ = item
    
    subtotal := price * quantity
    afterDiscount := subtotal - discount
    tax := afterDiscount * taxRate / 100
    total := afterDiscount + tax
    line1 := "Subtotal: $" + strconv.Itoa(subtotal)
    line2 := "Discount: $" + strconv.Itoa(discount)
    line3 := "Tax: $" + strconv.Itoa(tax)
    line4 := "Total: $" + strconv.Itoa(total)
    result := line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
    fmt.Println(result)
    

    The code uses the fmt and strconv packages.

    The details worth knowing

    • _ = item. This version never prints the item name, and Go refuses a variable that is declared and never used. Assigning it to the blank identifier _ counts as a use and discards the value.
    • Integer division truncates toward zero. 31 * 8 / 100 is 248 / 100, which is 2. The 0.48 is dropped, not rounded to the nearest.
    • Multiply, then divide. Writing afterDiscount / 100 * taxRate would divide first and lose the remainder, so for 31 the tax would be 0.
    • Converting each number. Every + joins strings, so each of the four numbers goes through strconv.Itoa. "Tax: $" + tax would not compile.
    • Checking the division. With the starting data, afterDiscount * taxRate / 100 evaluates to 2, while afterDiscount / 100 * taxRate evaluates to 0: the second form divides 31 by 100 first, which is 0.
    • Naming the steps. The lesson spells the rules out as separate variables on purpose: each line of code maps to one rule in the statement, which makes it easy to check.

    An alternative: one formatted call

    Once the four numbers exist, fmt.Printf("Subtotal: $%d\nDiscount: $%d\nTax: $%d\nTotal: $%d\n", subtotal, discount, tax, total) prints the same four rows without any strconv.Itoa call or +. Each %d takes the next argument, in order. The lesson builds the rows as strings instead, so every row is its own variable and the joining with "\n" is explicit.

    Complexity

    The program runs a fixed number of arithmetic operations and string joins, whatever the values are. Time and extra space are O(1) apart from the length of the output text, which is a few dozen characters.

    Edge cases and common mistakes

    • Taxing the wrong amount. The tax is charged on the discounted amount. With price := 10, quantity := 5, discount := 10 and taxRate := 10, the correct output is Subtotal: $50, Discount: $10, Tax: $4 and Total: $44. Using subtotal in the tax line would charge tax on the amount before the discount, which the rules do not allow.
    • No discount. With discount := 0 the output is Subtotal: $36, Discount: $0, Tax: $2, Total: $38.
    • Tax rounding down. With price := 10, quantity := 1 and discount := 0, the tax is $0 and the total is $10: 10 * 8 / 100 is 0.8, cut to 0.
    • No tax. With taxRate := 0 the tax row shows $0 and the total is 31.
    • A discount bigger than the subtotal. The code does not guard against it. With quantity := 1 and discount := 15, the output ends with Total: $-3. When the discount equals the subtotal, the total is 0. The lesson's rules do not ask for a guard.
    • The wrong number of newlines. Joining with fewer than three newlines puts two rows on one line.

    Testing

    • The starting data prints Subtotal: $36, Discount: $5, Tax: $2 and Total: $33.
    • discount := 0 prints Subtotal: $36, Discount: $0, Tax: $2 and Total: $38.
    • taxRate := 0 prints Subtotal: $36, Discount: $5, Tax: $0 and Total: $31.
    • quantity := 1 with discount := 12 prints Subtotal: $12, Discount: $12, Tax: $0 and Total: $0.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free