Sum the line totals of an order where each line has a quantity and unit price in Money.

← Operators and Expressions · Ref: Q1200

EK9 Money supports operator * with Integer for quantity multiplication and operator + for accumulation.

MONEY ARITHMETIC

  unitPrice <- 29.99#USD
  lineTotal <- unitPrice * quantity   Money * Integer = Money

ACCUMULATION

  Use a for loop with += to sum Money values:
  total <- 0.00#USD
  for item in items
    total += item.unitPrice * item.quantity

Money handles precision automatically with HALF_UP rounding. Same-currency operations work directly; cross-currency returns unset.

See Q1189 for money comparison. See Q35 for Money basics. See Q1139 for stream join.

Example

defines module qa.operators.moneyordertotal

  defines record

    OrderLine
      description as String: String()
      unitPrice as Money: Money()
      quantity as Integer: Integer()

      OrderLine() as pure
        ->
          theDescription as String
          theUnitPrice as Money
          theQuantity as Integer
        this.description :=: theDescription
        this.unitPrice :=: theUnitPrice
        this.quantity :=: theQuantity

      default operator

  defines program

    MoneyOrderTotalDemo()
      stdout <- Stdout()

      // === CREATE ORDER LINES ===

      lines <- List() of OrderLine
      lines += OrderLine("Widget", 29.99#USD, 3)
      lines += OrderLine("Gadget", 45.50#USD, 2)
      lines += OrderLine("Cable", 9.99#USD, 5)

      // === CALCULATE LINE TOTALS ===

      for line in lines
        lineTotal <- line.unitPrice * line.quantity
        stdout.println(`${line.description}: ${lineTotal}`)

      // === SUM ALL LINE TOTALS ===

      orderTotal <- 0.00#USD
      for line in lines
        orderTotal += line.unitPrice * line.quantity

      stdout.println(`Order total: ${orderTotal}`)

      // === VERIFY INDIVIDUAL TOTALS ===

      widgetTotal <- 29.99#USD * 3
      stdout.println(`Widget total: ${widgetTotal}`)

      gadgetTotal <- 45.50#USD * 2
      stdout.println(`Gadget total: ${gadgetTotal}`)

Common mistakes

E50060 — Money has no multiply() method — use the * operator directly on Money with an Integer or Float quantity. See ek9 -h E50060 for details.

Incorrect:

orderTotal += line.unitPrice.multiply(line.quantity)

Correct:

orderTotal += line.unitPrice * line.quantity
Other ways to ask this
  • Calculate order totals by multiplying Money by Integer quantities.
  • I need to compute the total cost of an order with multiple line items.
  • In Java I'd use BigDecimal.multiply for each line — how does EK9 handle Money arithmetic?
  • Given line items with quantity and unit price, sum up the order total.

Coming from another language?

Java: BigDecimal.multiply(quantity).setScale(2, RoundingMode.HALF_UP), then stream().reduce(BigDecimal.ZERO, BigDecimal::add). Python: Decimal * int. Rust: no built-in money. Go: no built-in money. EK9: unitPrice * quantity with automatic precision, += for accumulation.

Keywords: multiply, total, price, line item, arithmetic, accumulate, sum, money, quantity, order