Reference
Unit Conversions
Unit conversion utilities and decimal handling for all supported blockchains
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Unit conversion utilities and decimal handling for all supported blockchains
function toDisplayUnits(amount, decimals) {
return amount / Math.pow(10, decimals);
}
// Examples
toDisplayUnits(100000000, 8); // 1.0 BTC
toDisplayUnits(1000000, 6); // 1.0 USDC
toDisplayUnits(1000000000000000000, 18); // 1.0 ETH
function toBaseUnits(amount, decimals) {
return Math.floor(amount * Math.pow(10, decimals));
}
// Examples
toBaseUnits(1.0, 8); // 100000000 satoshis
toBaseUnits(1.0, 6); // 1000000 (USDC)
toBaseUnits(1.0, 18); // 1000000000000000000 wei
function toBaseUnitsSafe(amount, decimals) {
const [whole, fraction = ''] = amount.toString().split('.');
const paddedFraction = fraction.padEnd(decimals, '0').slice(0, decimals);
return BigInt(whole + paddedFraction);
}
// Examples
toBaseUnitsSafe('1.0', 18); // 1000000000000000000n
toBaseUnitsSafe('0.5', 18); // 500000000000000000n
toBaseUnitsSafe('1.23456789', 8); // 123456789n
// Satoshis to BTC
const btc = satoshis / 100000000;
// Or
const btc = satoshis / 1e8;
// BTC to Satoshis
const satoshis = btc * 100000000;
// Or
const satoshis = Math.floor(btc * 1e8);
// Examples
100000000 // satoshis → 1.0 BTC
50000000 // satoshis → 0.5 BTC
1234567 // satoshis → 0.01234567 BTC
// Same as Bitcoin
const ltc = litoshis / 1e8;
const litoshis = Math.floor(ltc * 1e8);
const doge = shibes / 1e8;
const shibes = Math.floor(doge * 1e8);
// Important: DOGE dust limit
const DOGE_DUST_LIMIT = 100000000; // 1 DOGE
if (shibes < DOGE_DUST_LIMIT) {
console.warn('Amount below dust limit');
}
const dash = duffs / 1e8;
const duffs = Math.floor(dash * 1e8);
const bch = satoshis / 1e8;
const satoshis = Math.floor(bch * 1e8);
const zec = zatoshis / 1e8;
const zatoshis = Math.floor(zec * 1e8);
// Wei to ETH
const eth = wei / 1000000000000000000;
// Or
const eth = wei / 1e18;
// Using ethers.js library
import { ethers } from 'ethers';
const eth = ethers.utils.formatEther(wei);
const wei = ethers.utils.parseEther(eth);
// Gwei to ETH (for gas prices)
const ethFromGwei = gwei / 1000000000;
// Or
const ethFromGwei = gwei / 1e9;
// Wei to Gwei
const gwei = wei / 1e9;
// Examples
1000000000000000000 // wei → 1.0 ETH
1000000000 // wei → 1.0 gwei
25500000000 // wei → 25.5 gwei (typical gas price)
// Gwei to Wei
function gweiToWei(gwei) {
return Math.floor(gwei * 1e9);
}
// Wei to Gwei
function weiToGwei(wei) {
return wei / 1e9;
}
// Examples
gweiToWei(25.5); // 25500000000 wei
weiToGwei(25500000000); // 25.5 gwei
| Token | Decimals | Example Conversion |
|---|---|---|
| USDC | 6 | 1000000 → 1.0 USDC |
| USDT | 6 | 1000000 → 1.0 USDT |
| DAI | 18 | 1000000000000000000 → 1.0 DAI |
| WBTC | 8 | 100000000 → 1.0 WBTC |
| LINK | 18 | 1000000000000000000 → 1.0 LINK |
// Generic ERC-20 conversion
function convertToken(amount, decimals) {
return amount / Math.pow(10, decimals);
}
// Examples
convertToken(1000000, 6); // 1.0 USDC
convertToken(1000000000000000000, 18); // 1.0 DAI
convertToken(100000000, 8); // 1.0 WBTC
const rune = tor / 1e8;
const tor = Math.floor(rune * 1e8);
// Fixed fee
const THOR_FIXED_FEE = 2000000; // 0.02 RUNE
// CACAO conversion (10 decimals)
const cacao = baseUnits / 1e10;
const baseUnits = Math.floor(cacao * 1e10);
// Normalize CACAO to standard 1e8 format
function normalizeCacao(amount) {
return (amount / 1e10) * 1e8;
}
// Examples
10000000000 // → 1.0 CACAO (10 decimals)
100000000000 // → 10.0 CACAO (10 decimals)
// Normalization
normalizeCacao(10000000000); // → 100000000 (standard 1e8)
function getDecimals(asset) {
return asset === 'MAYA.CACAO' ? 10 : 8;
}
const atom = uatom / 1e6;
const uatom = Math.floor(atom * 1e6);
// Examples
1000000 // uatom → 1.0 ATOM
500000 // uatom → 0.5 ATOM
const kuji = ukuji / 1e6;
const ukuji = Math.floor(kuji * 1e6);
// YoctoNEAR to NEAR
const near = yoctoNEAR / 1e24;
// Or
const near = yoctoNEAR / 1000000000000000000000000;
// NEAR to YoctoNEAR
const yoctoNEAR = Math.floor(near * 1e24);
// Examples
1000000000000000000000000 // → 1.0 NEAR
5000000000000000000000000 // → 5.0 NEAR
// Using NEAR API library
import { utils } from 'near-api-js';
const near = utils.format.formatNearAmount(yoctoNEAR);
const yoctoNEAR = utils.format.parseNearAmount(near);
// Must fetch decimals from token metadata
async function getNEP141Decimals(contractId) {
const metadata = await contract.ft_metadata();
return metadata.decimals;
}
// Example: USDC on NEAR
const NEAR_USDC_DECIMALS = 6;
const usdc = amount / 1e6;
// TGas (TeraGas) conversions
const TGAS = 1000000000000; // 1 TGas = 10^12 gas
// Standard ft_transfer gas
const FT_TRANSFER_GAS = 30 * TGAS; // 30 TGas
// Attached deposit (always 1 yoctoNEAR for tokens)
const ATTACHED_DEPOSIT = '1';
const xrp = drops / 1e6;
const drops = Math.floor(xrp * 1e6);
// Reserve requirement
const XRP_RESERVE = 10000000; // 10 XRP in drops
const sol = lamports / 1e9;
const lamports = Math.floor(sol * 1e9);
// Using Solana web3.js
import { LAMPORTS_PER_SOL } from '@solana/web3.js';
const sol = lamports / LAMPORTS_PER_SOL;
const lamports = sol * LAMPORTS_PER_SOL;
const trx = sun / 1e6;
const sun = Math.floor(trx * 1e6);
const ada = lovelace / 1e6;
const lovelace = Math.floor(ada * 1e6);
// BPS to Percentage
function bpsToPercent(bps) {
return bps / 100;
}
// BPS to Decimal
function bpsToDecimal(bps) {
return bps / 10000;
}
// Percentage to BPS
function percentToBps(percent) {
return percent * 100;
}
// Examples
bpsToPercent(150); // 1.5%
bpsToDecimal(150); // 0.015
percentToBps(1.5); // 150 BPS
// Apply BPS fee to amount
function applyBpsFee(amount, feeBps) {
return Math.floor(amount * feeBps / 10000);
}
applyBpsFee(100000000, 30); // 300000 (0.3% of 1 BTC)
// ❌ Bad: Floating point precision issues
const result = 0.1 + 0.2; // 0.30000000000000004
// ✅ Good: Use integer arithmetic
function add(a, b, decimals) {
const aInt = Math.floor(a * Math.pow(10, decimals));
const bInt = Math.floor(b * Math.pow(10, decimals));
return (aInt + bInt) / Math.pow(10, decimals);
}
add(0.1, 0.2, 8); // 0.3 (exact)
// Using bignumber.js
import BigNumber from 'bignumber.js';
const amount = new BigNumber('1.23456789');
const baseUnits = amount.times(1e8).integerValue();
// 123456789
// Using ethers.js for Ethereum
import { ethers } from 'ethers';
const eth = '1.5';
const wei = ethers.utils.parseEther(eth);
// BigNumber { _hex: '0x14d1120d7b160000' }
const ethBack = ethers.utils.formatEther(wei);
// '1.5'
// Floor (always round down)
Math.floor(1.9999 * 1e8) / 1e8; // 1.99990000
// Ceil (always round up)
Math.ceil(1.0001 * 1e8) / 1e8; // 1.00010000
// Round (banker's rounding)
Math.round(1.5555 * 1e8) / 1e8; // 1.55550000
// Truncate decimals
function truncate(amount, decimals) {
const multiplier = Math.pow(10, decimals);
return Math.floor(amount * multiplier) / multiplier;
}
truncate(1.23456789, 6); // 1.234567
function formatAmount(amount, decimals, displayDecimals = 6) {
const converted = amount / Math.pow(10, decimals);
return converted.toFixed(displayDecimals);
}
formatAmount(100000000, 8, 2); // "1.00"
formatAmount(123456789, 8, 4); // "1.2346"
formatAmount(1000000, 6, 2); // "1.00"
function formatCompact(amount, decimals) {
const value = amount / Math.pow(10, decimals);
if (value >= 1e9) return (value / 1e9).toFixed(2) + 'B';
if (value >= 1e6) return (value / 1e6).toFixed(2) + 'M';
if (value >= 1e3) return (value / 1e3).toFixed(2) + 'K';
return value.toFixed(2);
}
formatCompact(100000000000000, 8); // "1000000.00" or "1.00M"
formatCompact(1000000000, 6); // "1000.00" or "1.00K"
function formatCurrency(amount, decimals, currency = 'USD') {
const value = amount / Math.pow(10, decimals);
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency,
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(value);
}
formatCurrency(9850000000, 6, 'USD'); // "$9,850.00"
function validateAmount(amount, decimals, min = 0, max = Infinity) {
const value = amount / Math.pow(10, decimals);
return value >= min && value <= max;
}
// Validate minimum swap amount
validateAmount(100000000, 8, 0.01, 100); // true (1 BTC)
validateAmount(100000, 8, 0.01, 100); // false (0.001 BTC < min)
const DUST_LIMITS = {
'BTC': 546, // satoshis
'LTC': 546, // litoshis
'DOGE': 100000000, // shibes (1 DOGE)
'BCH': 546 // satoshis
};
function isDust(amount, chain) {
return amount < (DUST_LIMITS[chain] || 0);
}
isDust(500, 'BTC'); // true (below 546 sats)
isDust(1000, 'BTC'); // false
| Chain | Decimals | Base Unit | 1 Unit in Base | Example |
|---|---|---|---|---|
| BTC | 8 | satoshi | 100,000,000 | 1 BTC = 100M sats |
| ETH | 18 | wei | 1,000,000,000,000,000,000 | 1 ETH = 1e18 wei |
| USDC | 6 | base | 1,000,000 | 1 USDC = 1M |
| NEAR | 24 | yoctoNEAR | 1e24 | 1 NEAR = 1e24 yocto |
| THOR.RUNE | 8 | tor | 100,000,000 | 1 RUNE = 100M tor |
| MAYA.CACAO | 10 | base | 10,000,000,000 | 1 CACAO = 10B |
| GAIA.ATOM | 6 | uatom | 1,000,000 | 1 ATOM = 1M uatom |
| SOL | 9 | lamport | 1,000,000,000 | 1 SOL = 1B lamports |
| XRP | 6 | drop | 1,000,000 | 1 XRP = 1M drops |
function convertQuoteAmount(quote) {
const fromDecimals = getAssetDecimals(quote.from_asset);
const toDecimals = getAssetDecimals(quote.to_asset);
return {
input: quote.amount / Math.pow(10, fromDecimals),
output: quote.expected_amount_out / Math.pow(10, toDecimals)
};
}
function calculateRate(inputAmount, outputAmount, inputDecimals, outputDecimals) {
const input = inputAmount / Math.pow(10, inputDecimals);
const output = outputAmount / Math.pow(10, outputDecimals);
return output / input;
}
// Example: 1 BTC → 65,000 USDC
calculateRate(100000000, 65000000000, 8, 6); // 65000