export function formatMoney(value) {
  if (value == null || Number.isNaN(value)) return "--";
  const abs = Math.abs(value);
  const sign = value < 0 ? "-" : "";
  if (abs >= 1e8) return `${sign}${(abs / 1e8).toFixed(2)}亿`;
  if (abs >= 1e4) return `${sign}${(abs / 1e4).toFixed(2)}万`;
  return `${sign}${abs.toFixed(2)}元`;
}

export function formatYuan(value) {
  if (value == null || Number.isNaN(value)) return "--";
  return `${Number(value).toFixed(2)}元`;
}

export function formatNumber(value, digits = 0) {
  return Number(value).toLocaleString("zh-CN", {
    minimumFractionDigits: digits,
    maximumFractionDigits: digits,
  });
}

export function formatPercent(value) {
  return `${(value * 100).toFixed(1)}%`;
}

export function clamp(n, min, max) {
  return Math.min(max, Math.max(min, n));
}
