全部经验 · Apps and UI · 从这里开始 · 术语表
Exact CKB-to-shannon conversion with BigInt实践来自一次修复
来自一次修复 · 核对结果:部分正确 · 核对于 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).
为什么重要: 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.
代码模式
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.同一提交中改动了测试
它与修复在一起,我们没有运行它。
- Hanssen0-Alter-Ego/ckb-vm-js:
tests/browser/playground.spec.js,tests/js/guided.test.js
- 概念
- Data encoding (molecule, hex, bigints), Capacity and cell size
- 证据
- 来自一次修复: 来自一个项目,已对照其提交核对
- 首次出现
- 2026-11-15
- 最近改动
- 2026-11-15
- 最近核对
- 2026-11-19
- 对照提交的核对结果
- 部分正确:核心属实,但有一部分表述松散或范围过宽
来源(1)
- 2026-11-15 · Hanssen0-Alter-Ego/ckb-vm-js · 这个项目的更多经验
更多关于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
引用这条经验
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.