Skip to content

Numbers ​

Chain values are whole numbers with the decimal point somewhere else, and CQL keeps them exact until you ask for a division.

Token amounts, balances and supplies arrive as very large integers. This page explains the two number types that hold them, how to make them readable, and the one place where rounding can happen.

Two types ​

bigint is a whole number of any size. Every uint256 and int256 a contract returns is a bigint, and so is $block. Nothing overflows. pow(2, 256) is fine.

decimal is an exact number with a fractional part — a whole number and a scale, never a floating-point value. 1.5 and 1.50 are the same decimal, and 0.1 + 0.2 is exactly 0.3.

When you add, subtract or multiply two of either type, you get an exact answer. When you mix the two types, the bigint becomes a decimal.

Making an amount readable ​

A token with 18 decimals reports one token as 1000000000000000000. format(x, decimals) moves the decimal point. stETH's three headline numbers all need it:

cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

from stETH as s
| project {
    block:        $block,
    ethPerShare:  format(s.getPooledEthByShares(1e18), 18),
    pooledEther:  format(s.getTotalPooledEther(), 18),
    totalShares:  format(s.getTotalShares(), 18)
  }

Open in workbench →

blockethPerSharepooledEthertotalShares
260427071.2447109157008669029761717.1353650365619361337842557.667190094063001578

format is exact. It never rounds. It strips trailing zeros, and it keeps at least one digit after the point. So format(1e18, 18) is 1.0 and format(1050000000000000000, 18) is 1.05. parse does the opposite: parse(1.05, 18) is 1050000000000000000. It drops any digits beyond the scale without complaint.

Division is the exception ​

bigint / bigint is whole-number division, truncated toward zero: 7 / 2 is 3. If either side is a decimal, CQL computes the quotient to 38 significant digits and rounds it half to even. If a run actually rounded something, the run says so with CQL4901.

That is why the quick start's (last / first - 1) / days * 100 divides two formatted values, which are decimals. It is also why its days is todecimal(totalseconds(until - since)) / 86400 rather than the whole-number division that would truncate two and a half days to two. avg() works the same way. % follows / on whole numbers and is not defined on decimals.

If you divide by a constant zero, CQL refuses the query before the run. If you divide by a zero that came from the data, that row gets null and the run continues.

Writing a number ​

  • 1000000, 1_000_000 — underscores between digit groups, for reading.
  • 1e18, 1.5e18 — exponents. The result is a bigint when it is whole, and a decimal otherwise. s.getPooledEthByShares(1e18) passes one whole share. The exponent can be at most 77. 1e77 is the largest power of ten that a uint256 holds. For a larger power of ten, use pow(10, n).
  • 1.05 — a decimal.
  • 0xff — not a number. Hex is bytes. tobigint(0xff) is the number 255.

See also ​

  • Types — every type, how it is written and compared.
  • Functions — format, parse, abs, pow, tobigint, todecimal.