ValulogDashboard

Why money APIs should use integers, not floats

Published

Most programming languages store 0.1 and 0.2 as binary floating-point numbers, which cannot represent those amounts exactly. Adding them does not reliably give 0.3. That is a poor foundation for prices, discounts and refunds. Valulog does not accept floats for money at all: every amount is an integer number of minor units, and every currency has a known exponent.

Minor units, not “pounds as a decimal”

In sterling, the minor unit is the penny, and the exponent is 2: £18.00 is sent as 1800. The same idea applies to USD and EUR. Some currencies differ. Japanese yen have exponent 0, so 500 means 500 yen, not 5.00. Kuwaiti dinar has exponent 3. Valulog looks the exponent up in a currency registry rather than assuming two decimal places.

If you send a non-integer, or a negative amount, the request is refused. There is no silent rounding of a float into a price.

Where floats go wrong in a basket

A 10 percent discount on three lines, each calculated as a float and rounded at the end, can disagree with 10 percent of the basket total. The customer sees one number at checkout and a different one on a partial refund. Integer arithmetic does not remove the need to decide how leftover pennies are shared, but it does make that decision explicit and repeatable. Valulog records the split on a frozen snapshot so a refund later uses the same figures.

What this does not do

Integer minor units do not calculate tax, and they are not tax advice. If you need to keep a tax basis alongside a line, that remains your field and your accountant’s question. Valulog also does not take the payment: your payment provider still moves the money.

Try it with a test key

Sandbox test keys (vl_test_) are free and do not need a card, with 1,000 operations a month. Paid plans are Starter at £49 a month and Growth at £199 a month, including any VAT due, for businesses only.

A 10% discount in integer pence (request body)
{
    "currency": "GBP",
    "items": [
      { "id": "line-1", "sku": "TEE", "unit_price_cents": 2000, "quantity": 2 },
      { "id": "line-2", "sku": "MUG", "unit_price_cents": 1000, "quantity": 1 }
    ],
    "discounts": [
      { "rule_id": "TENOFF", "type": "PERCENTAGE", "basis_points": 1000 }
    ]
  }
  // 10% of £50.00 is £5.00, stored as 500. Shared 400 / 100 across the two lines.
  // totals.final_total_cents is 4500, not a float.

Valulog is not a payment processor and does not give tax advice. Payments stay with your payment provider; Valulog records how prices, discounts and refunds were calculated.

Run this exact payload in Sandbox

10% off two T-shirts and a mug: calculate the basket, then refund 1 unit of line-1 from the frozen snapshot. Free test key, no card.

# 1. Calculate and freeze the basket (needs jq to read the transaction_id)
TX=$(curl -s https://api.valulog.com/v1/calculate \
  -H "Authorization: Bearer $VALULOG_KEY" \
  -H "Idempotency-Key: demo-tenoff-calc" \
  -H "Content-Type: application/json" \
  -d '{
  "currency": "GBP",
  "items": [
    {
      "id": "line-1",
      "sku": "TEE",
      "unit_price_cents": 2000,
      "quantity": 2
    },
    {
      "id": "line-2",
      "sku": "MUG",
      "unit_price_cents": 1000,
      "quantity": 1
    }
  ],
  "discounts": [
    {
      "rule_id": "TENOFF",
      "type": "PERCENTAGE",
      "basis_points": 1000,
      "application_base": "REMAINING_BALANCE"
    }
  ]
}' | jq -r .transaction_id)

# 2. Refund 1 unit(s) of line-1 from the frozen snapshot
curl https://api.valulog.com/v1/refunds \
  -H "Authorization: Bearer $VALULOG_KEY" \
  -H "Idempotency-Key: demo-tenoff-refund-1" \
  -H "Content-Type: application/json" \
  -d "{\"transaction_id\": \"$TX\", \"line_item_id\": \"line-1\", \"quantity\": 1, \"reason\": \"Customer return\"}"
Try it here first, no sign-in

The button keeps this payload in your browser and opens your dashboard (sign in with your email first if you are not already), where it is ready to run with your vl_test_ key.

Change the numbers and watch the refund

No sign-in. Edit the basket and the discount rules, then refund a line. This runs Valulog’s own pricing engine (engine 1.3.0) in your browser: nothing is sent or stored, and the IDs, hash and timestamps are generated for this simulation.

Basket (prices in pence)
LineSKUUnit priceQtyDiscountableRemove
line-1
line-2
Discount rules (applied in this order)

Result: how each discount was split

Refund part of the order

Refunds come from the frozen line, not today’s prices. Refund the same line again to see the over-refund guard; the last unit takes any leftover penny.

POST /v1/calculate · … response

…

Copies a ready-to-run curl script for this exact basket and refund, and keeps it on your dashboard. Free sandbox: no card, 1,000 operations a month.

Related