CKB Pulse

All lessons · Apps and UI · Start here · Glossary

Exact CKB-to-shannon conversion with BigIntWorkingFrom one fix

From one fix · Check: partly right · Checked 2026-11-19

Convert user-entered CKB amounts to shannons with BigInt string arithmetic: BigInt(whole) * 100000000n + BigInt(fraction.padEnd(8, '0')). Never use parseFloat(amount) * 1e8. First validate the input with /^\d+(\.\d{1,8})?$/, which rejects exponents, hex and commas, and reject any result above u64::MAX (0xffffffffffffffffn).

Why it matters: Float or Number arithmetic loses shannon precision for amounts past the safe integer range, such as 99999999999.99999999 CKB, and loose parsing accepts inputs like '1e3' or '0x100' that become the wrong capacity.

Pattern

Wrong: Math.round(parseFloat(amount) * 1e8). Preferred: BigInt(whole) * 100000000n + BigInt(fraction.padEnd(8, '0')), after checking /^\d+(\.\d{1,8})?$/ and an upper bound of 0xffffffffffffffffn.

A test changed in the same commit

It sits next to the fix. We did not run it.

Concepts
Data encoding (molecule, hex, bigints), Capacity and cell size
Evidence
From one fix: from one project, checked against its commit
First seen
2026-11-15
Last changed
2026-11-15
Last checked
2026-11-19
Check against the commit
partly right: a true core, with a part that is loose or too wide

Sources (1)

More about Apps and UI

All 9 in this topic

Do you maintain a project this lesson came from? Confirm or correct it

Cite this lesson
LusoCryptoLabs, "Exact CKB-to-shannon conversion with BigInt", CKB lessons, https://pulse.lusocryptolabs.com/learned/apps/exact-ckb-to-shannon-conversion-with-bigint, CC BY 4.0, last changed 2026-11-15.

Drawn from public commits and checked against the commit each one came from and the project's code today. Read the source before you rely on one. Text under CC BY 4.0: credit LusoCryptoLabs, CKB lessons, with a link to the lesson. Want a lesson changed or removed? Open an issue.