Function parseUnits
Parameters
- value: string | number
The string or number value (e.g., "1.5" or 1.5)
- decimals: number = 6
The number of decimals (default: 6 for KLV/KFI)
Returns bigint
Value in smallest units as bigint
Example
parseUnits('1') // 1000000n
parseUnits('1.5') // 1500000n
parseUnits('0.000001') // 1n
parseUnits(2.5) // 2500000n
parseUnits('100', 3) // 100000n (with 3 decimals)
// Throws error - too many decimals
parseUnits('1.1234567', 6) // Error: Too many decimal places (max 6)
// Throws error - invalid format
parseUnits('1.2.3') // Error: Invalid decimal valueSee
- formatUnits for the reverse operation
- parseKLV for KLV-specific parsing
- value: string | number
Parse a human-readable string to its smallest unit
Converts decimal amounts to blockchain smallest units. Throws an error if too many decimal places are provided.