formatNum
API Reference für die Methode formatNum zum Formatieren von Zahlen gemäß den Konventionen der locale
Überblick
Die Methode formatNum formatiert Zahlen gemäß lokalspezifischen Konventionen mithilfe der Internationalization API.
Sie berücksichtigt Dezimaltrennzeichen, Tausendertrennzeichen und Zahlensysteme automatisch basierend auf der Ziel‑locale.
const gt = new GT({ targetLocale: 'de' });
const formatted = gt.formatNum(1234.56, {
style: 'decimal',
minimumFractionDigits: 2
});
// Ergebnis: „1.234,56“ (deutsches Zahlenformat)Referenzen
Parameter
| Name | Typ | Beschreibung |
|---|---|---|
number | number | Die zu formatierende Zahl |
options? | NumberFormatOptions | Optionale Einstellungen für die Formatierung |
NumberFormatOptions
Erweitert Intl.NumberFormatOptions um eine zusätzliche Locale-Spezifikation:
| Name | Typ | Beschreibung |
|---|---|---|
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' | Wie die Währung angezeigt wird |
currencySign? | 'standard' | 'accounting' | Zu verwendendes Währungsvorzeichen |
unit? | string | Einheitenbezeichner (erforderlich, wenn style 'unit' ist) |
unitDisplay? | 'short' | 'narrow' | 'long' | Wie die Einheit angezeigt wird |
minimumIntegerDigits? | number | Mindestanzahl an Ganzzahlstellen (1–21) |
minimumFractionDigits? | number | Mindestanzahl an Nachkommastellen (0–20) |
maximumFractionDigits? | number | Höchstanzahl an Nachkommastellen (0–20) |
minimumSignificantDigits? | number | Mindestanzahl signifikanter Ziffern (1–21) |
maximumSignificantDigits? | number | Höchstanzahl signifikanter Ziffern (1–21) |
useGrouping? | boolean | 'always' | 'auto' | 'min2' | Ob Tausendertrennzeichen verwendet werden |
notation? | 'standard' | 'scientific' | 'engineering' | 'compact' | Notationsformat der Zahl |
compactDisplay? | 'short' | 'long' | Anzeigeart 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 | Rundungsinkrement |
trailingZeroDisplay? | 'auto' | 'stripIfInteger' | Ob nachgestellte Nullen angezeigt werden |
Rückgabe
string - Die formatierte Zahl gemäß den Konventionen der locale.
Beispiele
Grundlegende Zahlenformatierung
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 die deutsche Locale
console.log(gt.formatNum(1234.567, { locales: 'de-DE' }));
// Ausgabe: „1.234,567“
// Formatierung für die 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ährungen
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 Schreibweise
// Einfacher Prozentwert
console.log(gt.formatNum(0.1234, { style: 'percent' }));
// Ausgabe: „12 %“
// Prozentwert 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 Mio.“
// 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 API
Intl.NumberFormatfür optimale Performance und Genauigkeit - Für Währungsformatierung sind sowohl
style: 'currency'als auch ein gültigercurrency-Code erforderlich - Für Einheitenformatierung sind sowohl
style: 'unit'als auch ein gültigerunit-Bezeichner erforderlich
Nächste Schritte
- Sieh dir die Dokumentation zu
Intl.NumberFormatfür weitere options an - Siehe
format-currencyfür spezialisierte Währungsformatierung - Siehe
format-messagefür Nachrichtenformatierung mit Zahleninterpolation - Siehe das eigenständige
format-numzur Verwendung ohne GT-Instanz - Siehe
get-locale-propertiesfür locale-spezifische Formatierungsinformationen
Wie ist diese Anleitung?