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.
- Hanssen0-Alter-Ego/ckb-vm-js:
tests/browser/playground.spec.js,tests/js/guided.test.js
- 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)
- 2026-11-15 · Hanssen0-Alter-Ego/ckb-vm-js · more from this project
More about Apps and UI
- Occupied capacity depends on the actual lock's args length
- Pinning JS codecs and derivations to Rust test vectors
- Pre-sign checkers must mirror every on-chain comparison
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.