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
| Name | Typ | Beschreibung | 
|---|---|---|
| number | number | Die zu formatierende Zahl | 
| options? | NumberFormatOptions | Optionale Formatierungseinstellungen | 
NumberFormatOptions
Erweitert Intl.NumberFormatOptions um eine zusätzliche Angabe der locale:
| Name | Type | Description | 
|---|---|---|
| locales? | string | string[] | locales für die Formatierung überschreiben (Standard: locales der Instanz) | 
| style? | 'decimal' | 'currency' | 'percent' | 'unit' | Stil der Zahlenformatierung | 
| currency? | string | Währungscode (erforderlich, wenn style 'currency' ist) | 
| currencyDisplay? | 'symbol' | 'narrowSymbol' | 'code' | 'name' | Darstellung der Währung | 
| currencySign? | 'standard' | 'accounting' | Zu verwendendes Währungsvorzeichen | 
| unit? | string | Einheitenbezeichner (erforderlich, wenn style 'unit' ist) | 
| unitDisplay? | 'short' | 'narrow' | 'long' | Darstellung der Einheit | 
| minimumIntegerDigits? | number | Minimale Anzahl an Ganzzahldigits (1–21) | 
| minimumFractionDigits? | number | Minimale Anzahl an Nachkommastellen (0–20) | 
| maximumFractionDigits? | number | Maximale Anzahl an Nachkommastellen (0–20) | 
| minimumSignificantDigits? | number | Minimale Anzahl signifikanter Stellen (1–21) | 
| maximumSignificantDigits? | number | Maximale 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 | 100 | Rundungsschritt | 
| 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.NumberFormatfür optimale Leistung und Genauigkeit
- Für die Währungsformatierung sind sowohl style: 'currency'als auch ein gültigercurrency-Code erforderlich
- Für die Einheitenformatierung sind sowohl style: 'unit'als auch ein gültigerunit-Bezeichner erforderlich
Nächste Schritte
- Weitere Optionen finden Sie in der Dokumentation zu Intl.NumberFormat
- Siehe format-currencyfür spezielle Währungsformatierung
- Siehe format-messagefür Nachrichtenformatierung mit Zahleninterpolation
- Siehe das eigenständige format-numfür die Verwendung ohne GT-Instanz
- Siehe get-locale-propertiesfür locale-spezifische Formatierungsinformationen
Wie ist dieser Leitfaden?

