An intuitive and precise Ethereum converter tool, adept at handling wei, eth, and custom token units, all built on the robust bignumber.js.
Convert between Ethereum's wei and eth units. Support for custom token units conversion based on decimals. Generalized conversion utility for any Ethereum-based units available in the library.
npm install @web3swag/web3-units-converter
Import the library:
const converter = require('@web3swag/web3-units-converter');
To run test cases
npm test
const wei = converter.convertEthToWei('1.5'); // returns '1500000000000000000'
const eth = converter.convertWeiToEth('1500000000000000000'); // returns '1.5'
const wei = converter.convertTokenToWei('100', 8); // Assuming the token has 8 decimals, returns 10000000000
const tokenValue = converter.convertWeiToToken('10000000000', 6); // Assuming the token has 8 decimals, returns 10000
const gwei = converter.convertAnyToAnyUnits('1', 'eth', 'gwei');
Always pass the amount as a string to ensure precision.
The library is designed to throw errors for unsupported units or if the input amount is not a string.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
MIT License