export function formatRunCostLine(costUsd: number | undefined): string {
  return costUsd === undefined
    ? "本次成本: 未记录"
    : `本次成本: ${formatUsd(costUsd)}`;
}

export function formatInteger(value: number): string {
  return new Intl.NumberFormat("en-US").format(value);
}

export function formatUsd(value: number): string {
  const digits = value > 0 && value < 0.01 ? 4 : 2;
  return `$${value.toFixed(digits)}`;
}

export function decimalToNumberOrNull(value: unknown): number | null {
  if (value === null || value === undefined) return null;
  const numberValue = typeof value === "number" ? value : Number(value);
  if (!Number.isFinite(numberValue)) {
    throw new Error("invalid cost value");
  }
  return numberValue;
}
