InputNumber is an input component to provide numerical input.
import { InputNumber } from 'primeng/inputnumber';
InputNumber is used as a controlled input with ngModel property.
<p-inputnumber inputId="integeronly" [(ngModel)]="value1" />
<p-inputnumber [(ngModel)]="value2" mode="decimal" inputId="withoutgrouping" [useGrouping]="false" />
<p-inputnumber [(ngModel)]="value3" inputId="minmaxfraction" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="5" />
<p-inputnumber [(ngModel)]="value4" inputId="minmax" mode="decimal" [min]="0" [max]="100" />
InputNumber can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
<form [formGroup]="formGroup">
<p-inputnumber inputId="integeronly" formControlName="value" />
</form>
Localization information such as grouping and decimal symbols are defined with the locale property which defaults to the user locale.
<p-inputnumber [(ngModel)]="value1" inputId="locale-user" [minFractionDigits]="2" />
<p-inputnumber [(ngModel)]="value2" inputId="locale-us" mode="decimal" locale="en-US" [minFractionDigits]="2" />
<p-inputnumber [(ngModel)]="value3" inputId="locale-german" mode="decimal" locale="de-DE" [minFractionDigits]="2" />
<p-inputnumber [(ngModel)]="value4" inputId="locale-indian" mode="decimal" locale="en-IN" [minFractionDigits]="2" />
Currency formatting is specified by setting the mode option to currency and currency property. In addition currencyDisplay option allows how the currency is displayed, valid values are "symbol" (default) or "code".
<p-inputnumber [(ngModel)]="value1" inputId="currency-us" mode="currency" currency="USD" locale="en-US" />
<p-inputnumber [(ngModel)]="value2" mode="currency"inputId="currency-germany"currency="EUR" locale="de-DE" />
<p-inputnumber [(ngModel)]="value3" mode="currency" inputId="currency-india" currency="INR" currencyDisplay="code" locale="en-IN" />
<p-inputnumber [(ngModel)]="value4" mode="currency" inputId="currency-japan" currency="JPY" locale="jp-JP" />
Custom texts e.g. units can be placed before or after the input section with the prefix and suffix properties.
<p-inputnumber [(ngModel)]="value1" inputId="mile" suffix=" mi" />
<p-inputnumber [(ngModel)]="value2" inputId="percent" prefix="%" />
<p-inputnumber [(ngModel)]="value3"inputId="expiry" prefix="Expires in " suffix=" days" />
<p-inputnumber [(ngModel)]="value4" prefix="↑ " inputId="temperature" suffix="℃" [min]="0" [max]="40" />
Spinner buttons are enabled using the showButtons options and layout is defined with the buttonLayout. Default value is "stacked" whereas "horizontal" and "stacked" are alternatives. Note that even there are no buttons, up and down arrow keys can be used to spin the values with keyboard.
<p-inputnumber [(ngModel)]="value1" [showButtons]="true" inputId="stacked" mode="currency" currency="USD" />
<p-inputnumber [(ngModel)]="value2" mode="decimal" [showButtons]="true" inputId="minmax-buttons" [min]="0" [max]="100" />
<p-inputnumber [(ngModel)]="value3" [showButtons]="true" buttonLayout="horizontal" inputId="horizontal" spinnerMode="horizontal" [step]="0.25" mode="currency" currency="EUR">
<ng-template pTemplate="incrementbuttonicon">
<span class="pi pi-plus"></span>
</ng-template>
<ng-template pTemplate="decrementbuttonicon">
<span class="pi pi-minus"></span>
</ng-template>
</p-inputnumber>
Buttons can also placed vertically by setting buttonLayout as vertical.
<p-inputnumber [(ngModel)]="value1" [showButtons]="true" buttonLayout="vertical" spinnerMode="vertical" inputId="vertical" [inputStyle]="{ width: '3rem' }">
<ng-template pTemplate="incrementbuttonicon">
<span class="pi pi-plus"></span>
</ng-template>
<ng-template pTemplate="decrementbuttonicon">
<span class="pi pi-minus"></span>
</ng-template>
</p-inputnumber>
A floating label appears on top of the input field when focused. Visit FloatLabel documentation for more information.
<p-floatlabel>
<p-inputnumber [(ngModel)]="value1" inputId="over_label" mode="currency" currency="USD" locale="en-US" />
<label for="over_label">Over Label</label>
</p-floatlabel>
<p-floatlabel variant="in">
<p-inputnumber [(ngModel)]="value2" inputId="in_label" mode="currency" currency="USD" locale="en-US" />
<label for="in_label">In Label</label>
</p-floatlabel>
<p-floatlabel variant="on">
<p-inputnumber [(ngModel)]="value3" inputId="on_label" mode="currency" currency="USD" locale="en-US" />
<label for="on_label">On Label</label>
</p-floatlabel>
IftaLabel is used to create infield top aligned labels. Visit IftaLabel documentation for more information.
<p-iftalabel>
<p-inputnumber [(ngModel)]="value" inputId="price_input" mode="currency" currency="USD" locale="en-US" />
<label for="price_input">Price</label>
</p-iftalabel>
InputNumber provides small and large sizes as alternatives to the base.
<p-inputnumber [(ngModel)]="value1" size="small" placeholder="Small" mode="currency" currency="USD" locale="en-US" />
<p-inputnumber [(ngModel)]="value2" placeholder="Normal" mode="currency" currency="USD" locale="en-US" />
<p-inputnumber [(ngModel)]="value3" size="large" placeholder="Large" mode="currency" currency="USD" locale="en-US" />
Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.
<p-inputnumber variant="filled" [(ngModel)]="value1" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-inputnumber inputId="integeronly" class="ng-invalid ng-dirty" [(ngModel)]="value1" />
When disabled is present, the element cannot be edited and focused.
<p-inputnumber inputId="integeronly" [disabled]="true" prefix="%" [(ngModel)]="value1" />
Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledBy, ariaLabel props. The input element uses spinbutton role in addition to the aria-valuemin, aria-valuemax and aria-valuenow attributes.
<label for="price">Price</label>
<p-inputnumber inputId="price" />
<span id="label_number">Number</span>
<p-inputnumber ariaLabelledBy="label_number" />
<p-inputnumber ariaLabel="Number" />
Key | Function |
---|---|
tab | Moves focus to the input. |
up arrow | Increments the value. |
down arrow | Decrements the value. |
home | Set the minimum value if provided. |
end | Set the maximum value if provided. |