Lattice Grid

blog

Exact big numbers in a data grid, 1.124

A JavaScript number is exact up to 9,007,199,254,740,992, which is 2^53. Past that it rounds. Token amounts in wei, ledger balances in minor units and IDs from a database all live far beyond it.

Put one in a grid and the last digits change without a sound. Nothing throws and nothing warns you on screen. Then a sort orders by the rounded value, a filter compares against it, a total adds it and an export writes it out, and the same error is repeated at every step.

Exact big numbers, end to end

Set a column’s type to 'bigint' and every digit is kept. That holds in the cells, in sorting, in filters, in totals and in exports. It also holds in derived grids built from the column, such as groups and the totals under them. Ordinary number columns are unchanged, so you opt in one column at a time.

A column takes BigInt values or decimal strings, negative ones too. A whole number is kept as a BigInt and a fixed-point value as a canonical decimal string, so equal values group together.

  • Sorting is by exact value, in both directions, with empty values last.
  • Filters compare exact values. Give an operand as a decimal string or a BigInt.
  • Totals are exact. A sum is BigInt arithmetic, and min, max and count never round.
  • Exports keep every digit. CSV and the clipboard show them all, and Excel holds the cell as text, because a spreadsheet number is a double too.

Raw units are rarely what people want to read. The scale option in typeOptions moves the point that many digits left, so wei reads as ETH and USDC base units read as dollars. The stored value, the sort, the filters and the totals stay in raw units, so nothing is rounded.

const grid = createGrid(el, {
  rowKey: 'id',
  grandTotalRow: true,
  columns: [
    { field: 'id' },
    { field: 'wei', type: 'bigint', typeOptions: { scale: 18 }, total: 'sum' },
    { field: 'raw', type: 'bigint', typeOptions: { grouping: false }, total: 'sum' },
  ],
  rows: [
    { id: 1, wei: 1500000000000000000n, raw: 1500000000000000000n },
    { id: 2, wei: '1000000000000000001', raw: '1000000000000000001' },
    { id: 3, wei: 1000000000000000002n, raw: 1000000000000000002n },
  ],
});

Here the wei column shows 1.5 for the first row, and the one-wei differences in the other two rows are still there when you sort. decimals fixes the fraction digits shown, and grouping: false drops the separators.

To see it on real chain data, open the Ethereum demo. Sort it, filter it and read the totals.

Typed number filters

In a number column’s filter-row box, type the comparison you mean. >105000, <=10 and !=0 each apply that operator, and a range such as 100..500 keeps the values between the two ends. The box names the operator in the picker, so you can see what it is doing. A value with no operator keeps the column’s current operator.

It works on bigint columns as well, so a threshold on a token balance is one short entry in the filter row.

What Enter does after an edit

After you commit a cell edit, Enter used to do one thing. Now edit.enterAfterCommit lets you choose:

  • 'edit' opens the next row’s editor in the same column. This is the default.
  • 'move' commits and moves down without opening an editor, as a spreadsheet does.
  • 'stay' commits and stays on the cell.
const grid = createGrid(el, {
  rowKey: 'id',
  rows,
  columns,
  edit: { enabled: true, enterAfterCommit: 'move' },
});

One change to know about: edit: true on the grid now makes every data column editable. To keep columns read-only on a grid that sets edit: true, set edit: false on each, or use columnDefaults: { edit: false }. Computed columns are unaffected.

Also in this release

Links and buttons inside custom-rendered cells now respond to a normal click. Pivot grids also show subtotals at every group level.

Try it

Start with the Ethereum demo, or paste your own numbers into the sandbox. To add the grid to a project:

npm install @toclocoinc/lattice-grid

The changelog has the full list.

Read next

All posts RSS