GT ClassMethodsFormatting

formatNum

API Reference für die Methode formatNum zum Formatieren von Zahlen gemäß den Konventionen der locale

Überblick

Die Methode formatNum formatiert Zahlen gemäß locale-spezifischen Konventionen mithilfe der Internationalization API. Sie berücksichtigt Dezimaltrennzeichen, Tausendertrennzeichen und Zahlensysteme automatisch auf Basis der Ziel-locale.

const gt = new GT({ targetLocale: 'de' });

const formatted = gt.formatNum(1234.56, {
  style: 'decimal',
  minimumFractionDigits: 2
});
// Gibt zurück: "1.234,56" (deutsche Zahlformatierung)

Referenzen

Parameter

NameTypBeschreibung
numbernumberDie zu formatierende Zahl
options?NumberFormatOptionsOptionale Formatierungseinstellungen

NumberFormatOptions

Erweitert Intl.NumberFormatOptions um eine zusätzliche Angabe der locale:

NameTypeDescription
locales?string | string[]locales für die Formatierung überschreiben (Standard: locales der Instanz)
style?'decimal' | 'currency' | 'percent' | 'unit'Stil der Zahlenformatierung
currency?stringWährungscode (erforderlich, wenn style 'currency' ist)
currencyDisplay?'symbol' | 'narrowSymbol' | 'code' | 'name'Darstellung der Währung
currencySign?'standard' | 'accounting'Zu verwendendes Währungsvorzeichen
unit?stringEinheitenbezeichner (erforderlich, wenn style 'unit' ist)
unitDisplay?'short' | 'narrow' | 'long'Darstellung der Einheit
minimumIntegerDigits?numberMinimale Anzahl an Ganzzahldigits (1–21)
minimumFractionDigits?numberMinimale Anzahl an Nachkommastellen (0–20)
maximumFractionDigits?numberMaximale Anzahl an Nachkommastellen (0–20)
minimumSignificantDigits?numberMinimale Anzahl signifikanter Stellen (1–21)
maximumSignificantDigits?numberMaximale Anzahl signifikanter Stellen (1–21)
useGrouping?boolean | 'always' | 'auto' | 'min2'Ob Gruppierungstrennzeichen verwendet werden
notation?'standard' | 'scientific' | 'engineering' | 'compact'Zahlennotation
compactDisplay?'short' | 'long'Darstellungsart der kompakten Notation
signDisplay?'auto' | 'never' | 'always' | 'exceptZero'Wann das Vorzeichen angezeigt wird
roundingMode?'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven'Rundungsmodus
roundingIncrement?1 | 2 | 5 | 10 | 20 | 25 | 50 | 100Rundungsschritt
trailingZeroDisplay?'auto' | 'stripIfInteger'Ob nachgestellte Nullen angezeigt werden

Rückgabewert

string - Die formatierte Zahl gemäß den Konventionen der jeweiligen locale.


Beispiele

Grundlegende Nummernformatierung

import { GT } from 'generaltranslation';

const gt = new GT({ targetLocale: 'en-US' });

// Einfache Dezimalformatierung
console.log(gt.formatNum(1234.567));
// Ausgabe: „1,234.567“

// Formatierung für deutsche Locale
console.log(gt.formatNum(1234.567, { locales: 'de-DE' }));
// Ausgabe: „1.234,567“

// Formatierung für französische Locale  
console.log(gt.formatNum(1234.567, { locales: 'fr-FR' }));
// Ausgabe: „1 234,567“

Währungsformatierung

// US-Dollar-Formatierung
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'USD'
}));
// Ausgabe: "$1,234.56"

// Euro-Formatierung mit deutscher Locale
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'EUR',
  locales: 'de-DE'
}));
// Ausgabe: "1.234,56 €"

// Anzeigeoptionen für Währungsdarstellung
console.log(gt.formatNum(1234.56, {
  style: 'currency',
  currency: 'USD',
  currencyDisplay: 'code'
}));
// Ausgabe: "USD 1,234.56"

// Buchhaltungsformat (Klammern für negative Werte)
console.log(gt.formatNum(-1234.56, {
  style: 'currency',
  currency: 'USD',
  currencySign: 'accounting'
}));
// Ausgabe: "($1,234.56)"

Prozent- und wissenschaftliche Notation

// Einfache Prozentangabe
console.log(gt.formatNum(0.1234, { style: 'percent' }));
// Ausgabe: „12 %“

// Prozentangabe mit Dezimalstellen
console.log(gt.formatNum(0.1234, {
  style: 'percent',
  minimumFractionDigits: 1,
  maximumFractionDigits: 2
}));
// Ausgabe: „12,34 %“

// Kompakte Notation
console.log(gt.formatNum(1234567, { notation: 'compact' }));
// Ausgabe: „1,2 M“

// Wissenschaftliche Notation
console.log(gt.formatNum(1234567, { notation: 'scientific' }));
// Ausgabe: „1,235E6“

Hinweise

  • Die Zahlenformatierung folgt automatisch den lokalspezifischen Konventionen
  • Die Methode verwendet die browsernative Intl.NumberFormat für optimale Leistung und Genauigkeit
  • Für die Währungsformatierung sind sowohl style: 'currency' als auch ein gültiger currency-Code erforderlich
  • Für die Einheitenformatierung sind sowohl style: 'unit' als auch ein gültiger unit-Bezeichner erforderlich

Nächste Schritte

Wie ist dieser Leitfaden?

formatNum