bitcoinFormatting.ts raw

   1  import { BITCOIN_DISPLAY_FORMAT_BIP177 } from "src/constants";
   2  import { BitcoinDisplayFormat } from "src/types";
   3  
   4  /**
   5   * Utility function to format Bitcoin amounts as a string
   6   * @param amountMsat - Amount in millisatoshis
   7   * @param displayFormat - Display format (required)
   8   * @param showSymbol - Whether to show the symbol/unit
   9   */
  10  export function formatBitcoinAmount(
  11    amountMsat: number,
  12    displayFormat: BitcoinDisplayFormat,
  13    showSymbol: boolean = true
  14  ): string {
  15    const sats = Math.floor(amountMsat / 1000);
  16    const formattedNumber = new Intl.NumberFormat().format(sats);
  17  
  18    if (!showSymbol) {
  19      return formattedNumber;
  20    }
  21  
  22    if (displayFormat === BITCOIN_DISPLAY_FORMAT_BIP177) {
  23      return `₿${formattedNumber}`;
  24    } else {
  25      return `${formattedNumber} sats`;
  26    }
  27  }
  28