Intl.NumberFormat
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
* Some parts of this feature may have varying levels of support.
Das Intl.NumberFormat
-Objekt ermöglicht sprachabhängige Zahlenformatierung.
Probieren Sie es aus
const number = 123456.789;
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// Expected output: "123.456,79 €"
// The Japanese yen doesn't use a minor unit
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// Expected output: "¥123,457"
// Limit to three significant digits
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// Expected output: "1,23,000"
Konstruktor
Intl.NumberFormat()
-
Erstellt ein neues
NumberFormat
-Objekt.
Statische Methoden
Intl.NumberFormat.supportedLocalesOf()
-
Gibt ein Array mit denjenigen der angegebenen Locales zurück, die unterstützt werden, ohne auf die Standard-Locale der Laufzeitumgebung zurückgreifen zu müssen.
Instanzeigenschaften
Diese Eigenschaften sind auf Intl.NumberFormat.prototype
definiert und werden von allen Intl.NumberFormat
-Instanzen geteilt.
Intl.NumberFormat.prototype.constructor
-
Die Konstruktorfunktion, die das Instanzobjekt erstellt hat. Für
Intl.NumberFormat
-Instanzen ist der ursprüngliche Wert derIntl.NumberFormat
-Konstruktor. Intl.NumberFormat.prototype[Symbol.toStringTag]
-
Der ursprüngliche Wert der
[Symbol.toStringTag]
-Eigenschaft ist der String"Intl.NumberFormat"
. Diese Eigenschaft wird inObject.prototype.toString()
verwendet.
Instanzmethoden
Intl.NumberFormat.prototype.format()
-
Getter-Funktion, die eine Zahl entsprechend der Locale und den Formatierungsoptionen dieses
Intl.NumberFormat
-Objekts formatiert. Intl.NumberFormat.prototype.formatRange()
-
Getter-Funktion, die einen Bereich von Zahlen entsprechend der Locale und den Formatierungsoptionen des
Intl.NumberFormat
-Objekts formatiert, von dem die Methode aufgerufen wird. Intl.NumberFormat.prototype.formatRangeToParts()
-
Gibt ein
Array
von Objekten zurück, das den Bereich der Zahlstrings in Teilen repräsentiert, die für benutzerdefinierte locale-abhängige Formatierung verwendet werden können. Intl.NumberFormat.prototype.formatToParts()
-
Gibt ein
Array
von Objekten zurück, das den Zahlstring in Teilen repräsentiert, die für benutzerdefinierte locale-abhängige Formatierung verwendet werden können. Intl.NumberFormat.prototype.resolvedOptions()
-
Gibt ein neues Objekt mit Eigenschaften zurück, die die Locale- und Kollationsoptionen widerspiegeln, die während der Initialisierung des Objekts berechnet wurden.
Beispiele
Grundlegende Verwendung
Bei einfacher Verwendung ohne Angabe einer Locale wird ein formatierter String in der Standard-Locale und mit Standardoptionen zurückgegeben.
const number = 3500;
console.log(new Intl.NumberFormat().format(number));
// '3,500' if in US English locale
Verwendung von Locales
Dieses Beispiel zeigt einige der Variationen in lokalisierten Zahlenformaten. Um das Format der Sprache zu erhalten, die in der Benutzeroberfläche Ihrer Anwendung verwendet wird, geben Sie diese Sprache (und eventuell einige Fallback-Sprachen) mit dem locales
-Argument an:
const number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(new Intl.NumberFormat("de-DE").format(number));
// 123.456,789
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(new Intl.NumberFormat("ar-EG").format(number));
// ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(new Intl.NumberFormat("en-IN").format(number));
// 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(number));
// 一二三,四五六.七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(new Intl.NumberFormat(["ban", "id"]).format(number));
// 123.456,789
Verwendung von Optionen
Die Ergebnisse können mit dem options
-Argument angepasst werden:
const number = 123456.789;
// request a currency format
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// ¥123,457
// limit to three significant digits
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// 1,23,000
// Formatting with units
console.log(
new Intl.NumberFormat("pt-PT", {
style: "unit",
unit: "kilometer-per-hour",
}).format(50),
);
// 50 km/h
console.log(
(16).toLocaleString("en-GB", {
style: "unit",
unit: "liter",
unitDisplay: "long",
}),
);
// 16 litres
Für eine vollständige Liste der Optionen, siehe die Seite Intl.NumberFormat()
constructor.
Spezifikationen
Specification |
---|
ECMAScript® 2025 Internationalization API Specification # numberformat-objects |
Browser-Kompatibilität
BCD tables only load in the browser