For the complete documentation index, see llms.txt. This page is also available as Markdown.

Unit Conversion

Learn how to convert between different unit denominations in Stacks. Stacks uses microSTX as its base unit, where 1 STX = 1,000,000 microSTX. Proper unit conversion is essential for displaying amounts to users and processing transactions.

Basic conversions

Convert between STX and microSTX using simple conversion functions.

basic-conversions.ts
// Convert STX to microSTX
function stxToMicroStx(stx: number | string): bigint {
  const stxAmount = typeof stx === 'string' ? parseFloat(stx) : stx;
  return BigInt(Math.floor(stxAmount * 1_000_000));
}

// Convert microSTX to STX
function microStxToStx(microStx: number | bigint | string): string {
  const amount = BigInt(microStx);
  const stx = Number(amount) / 1_000_000;
  return stx.toFixed(6).replace(/\.?0+$/, '');
}

// Usage examples
const microStx = stxToMicroStx(1.5);      // 1500000n
const stx = microStxToStx(1500000);       // "1.5"

Precision-safe handling

Handle large numbers without precision loss using BigInt operations.

Token conversions

Handle tokens with different decimal places using a flexible converter class.

Display formatting

Format amounts for user interfaces with localization support.

Last updated

Was this helpful?