synthtraffic Get started
Docs menu

Expressions

Math

Money calculations should stay readable in JSON and repeat exactly in tests. Synthtraffic preserves decimal scale instead of exposing binary floating-point noise.

The result

{"unitPrice":19.99,"quantity":3,"subtotal":59.97,"discountLeft":9.95,"pricePerItem":19.99,"roundedScore":1.24}

Calculate an order

YAML order-math.yaml
generators:
  - name: orders
    config:
      maxEvents: 1
    value:
      unitPrice: 19.99
      quantity: 3
      subtotal: =multiply($unitPrice, $quantity)
      discountLeft: =subtract(10.00, 0.05)
      pricePerItem: =divide($subtotal, $quantity, round=2)
      roundedScore: =round(1.236, 2)

Arithmetic functions

sum: =add(1, 2, 3)
difference: =subtract(10, 3)
product: =multiply(2, 3, 4)
ratio: =divide(10, 4)
power: =pow(2, 8)
rounded: =round(1.236, 2)
minimum: =min(3, 1, 2)
maximum: =max(3, 1, 2)
{"sum":6,"difference":7,"product":24,"ratio":2.5,"power":256,"rounded":1.24,"minimum":1,"maximum":3}
  • add(value, ...) and multiply(value, ...) require at least two numbers.
  • subtract(left, right), divide(left, right), and pow(base, exponent) take exactly two numbers.
  • round(value, digits) requires a non-negative integer number of digits.
  • min(value, ...) and max(value, ...) accept one or more numbers. A single list is also accepted, which is useful for $items.price.

Division by literal zero fails validation. A divisor that evaluates to zero stops evaluation with a division-by-zero error.

Decimal precision

Fractional literals carry their written scale into arithmetic. Values produced with round=N also keep that scale:

exact: =add(0.1, 0.2)
money: =multiply(19.99, 3)
average: =divide(10, 3, round=2)
{"exact":0.3,"money":59.97,"average":3.33}

round=N is available on all arithmetic functions except the round wrapper itself. It is also available on sum and avg.

List aggregates—sum, count, and avg—are covered with nested data in Collections.

Next: compare values and choose branches.