DeFiCh/wallet

View on GitHub
mobile-app/app/hooks/useUnitSuffix.ts

Summary

Maintainability
A
0 mins
Test Coverage
import BigNumber from "bignumber.js";

export function useUnitSuffix(
  units: Record<number, string>,
  value: string
): string {
  const _value = new BigNumber(value);
  const places = _value.e !== null ? Math.floor(_value.e / 3) : 0;
  const suffix = `${units[places * 3] ?? ""}`;

  return _value.dividedBy(1000 ** places).toFormat(2, {
    decimalSeparator: ".",
    suffix: suffix,
  });
}