Format Number
<pc-format-number> Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Formats a number using the specified locale and options.
Localisation is handled by the browser’s Intl.DateTimeFormat API. No language packs are required.
<div class="format-number-overview">
<pc-format-number value="1000"></pc-format-number>
<br /><br />
<pc-input
label="Number to format"
type="number"
value="1000"
style="max-inline-size: 180px"
></pc-input>
</div>
<script>
const container = document.querySelector(".format-number-overview");
const formatter = container.querySelector("pc-format-number");
const input = container.querySelector("pc-input");
input.addEventListener("pc-input", () => (formatter.value = input.value || 0));
</script> Demos#
Percentages#
To get the value as a percentage, set the type attribute to percent.
Localisation#
Use the lang attribute to set the number formatting locale.
SI conventions #
Add the use-si attribute to change the formatting of the component to follow SI conventions.
This property is experimental and does not work on currencies.
Currency#
To format a number as a monetary value, set the type attribute to currency and set the currency attribute to the desired ISO 4217 currency code. You should also specify the lang attribute to ensure that the number and currency symbol is formatted correctly for the target locale.
Eigenschaften#
| Name | Beschreibung | Reflektiert | Standard |
|---|---|---|---|
value | The number to format. Typ: number |
| 0 |
type | The formatting style to use. Typ: "currency" | "decimal" | "percent" |
| "decimal" |
withoutGroupingwithout-grouping | Turns off grouping separators. Typ: boolean |
| false |
currency | The ISO 4217 currency code to use when formatting. Typ: string |
| "USD" |
currencyDisplaycurrency-display | Specifies how to display the currency. Typ: "symbol"
| "narrowSymbol"
| "code"
| "name" |
| "symbol" |
minimumIntegerDigitsminimum-integer-digits | The minimum number of integer digits to use. Possible values are 1–21. Typ: number | undefined |
| ‐ |
minimumFractionDigitsminimum-fraction-digits | The minimum number of fraction digits to use. Possible values are 0–100. Typ: number | undefined |
| ‐ |
maximumFractionDigitsmaximum-fraction-digits | The maximum number of fraction digits to use. Possible values are 0–100. Typ: number | undefined |
| ‐ |
minimumSignificantDigitsminimum-significant-digits | The minimum number of significant digits to use. Possible values are 1–21. Typ: number | undefined |
| ‐ |
maximumSignificantDigitsmaximum-significant-digits | The maximum number of significant digits to use,. Possible values are 1–21. Typ: number | undefined |
| ‐ |
useSIuse-si | Forces the component to use the SI standard for formatting. This overrides locale‐specific rules. Typ: boolean |
| false |
updateComplete | Ein schreibgeschütztes Promise, das erfüllt ist, sobald die Komponente fertig aktualisiert wurde. | ‐ |
Erfahre mehr über Attribute und Eigenschaften.
Importieren#
Wenn du den Autoloader oder den Standard‐Loader nutzt, kannst du diesen Abschnitt überspringen. Falls du „Cherry Picking“ betreibst, kannst du die folgenden Snippets verwenden, um diese Komponente zu importieren.
Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in dein HTML ein.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/format-number/format-number.js"></script> Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/format-number/format-number.js"; Um diese Komponente manuell via npm zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "placer-toolkit/dist/components/format-number/format-number.js";