Go: Print a receipt line

Printf is how programs print neat, aligned numbers.

What to build

A program that prints one line of a shop receipt.

Requirements

  • Read the item name, the quantity and the price of one item.
  • Print ITEM xQTY @ PRICE = TOTAL with the price and the total shown with two decimals.
  • On the second line print Items: QTY.

Input

One line: item (one word), quantity (whole number) and price (decimal number).

Output

Two lines.

Constraints

1 ≤ quantity ≤ 100; 0 < price ≤ 1000, with at most two decimals.

Example

Input:

pen 3 1.5

Output:

pen x3 @ 1.50 = 4.50
Items: 3

Why it works

The quantity is a whole number and the price has decimals, so the total needs float64(qty). %.2f rounds to two decimals in the output.

Examples

Example 1 · Input
pen 3 1.5
Expected output
pen x3 @ 1.50 = 4.50
Items: 3
Example 2 · Input
notebook 12 3.25
Expected output
notebook x12 @ 3.25 = 39.00
Items: 12

Hints

Hint 1

Read a string, an int and a float64 with fmt.Scan(&item, &qty, &price).

Hint 2

Use Printf with %s, %d and %.2f. The total is float64(qty) * price.

Solution

Show one correct solution (try it yourself first)
package main

import "fmt"

func main() {
	var item string
	var qty int
	var price float64
	fmt.Scan(&item, &qty, &price)
	total := float64(qty) * price
	fmt.Printf("%s x%d @ %.2f = %.2f\n", item, qty, price, total)
	fmt.Printf("Items: %d\n", qty)
}

▶ Open this solution in the compiler

Lesson: Your first Go program →