How to use the @fluent/bundle/compat.FluentNumber function in @fluent/bundle

To help you get started, we’ve selected a few @fluent/bundle examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coralproject / talk / src / core / client / framework / lib / i18n / types / FluentShortNumber.ts View on Github external
function formatShortNumber(n: number, format: Pattern, bundle: FluentBundle) {
  const lastIndexOf0 = format.lastIndexOf("0");
  const unit = format.substr(lastIndexOf0 + 1);
  const rest = format.substr(0, lastIndexOf0 + 1);
  const splitted = rest.split(".");
  const digits = splitted[0].length;
  const fractalDigits = (splitted.length > 1 && splitted[1].length) || 0;
  const threshold = Math.pow(10, digits);
  while (n > threshold) {
    n /= 10;
  }
  const formattedNumber = new FluentNumber(n, {
    maximumFractionDigits: fractalDigits,
  }).toString(bundle);
  return `${formattedNumber}${unit}`;
}