InputNumber is an input component to provide numerical input.
import { InputNumberModule } from 'primeng/inputnumber';
InputNumber is used as a controlled input with ngModel property.
<p-inputNumber inputId="integeronly" [(ngModel)]="value1"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value2" mode="decimal" inputId="withoutgrouping" [useGrouping]="false"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value3" inputId="minmaxfraction" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="5"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value4" inputId="minmax" inputId="minmax" mode="decimal" [min]="0" [max]="100"> </p-inputNumber>
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"></p-inputNumber>
</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>
<p-inputNumber [(ngModel)]="value2" inputId="locale-us" mode="decimal" locale="en-US" [minFractionDigits]="2"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value3" inputId="locale-german" mode="decimal" locale="de-DE" [minFractionDigits]="2"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value4" inputId="locale-indian" mode="decimal" locale="en-IN" [minFractionDigits]="2"> </p-inputNumber>
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>
<p-inputNumber [(ngModel)]="value2" mode="currency" inputId="currency-germany" currency="EUR" locale="de-DE"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value3" mode="currency" inputId="currency-india" currency="INR" currencyDisplay="code" locale="en-IN"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value4" mode="currency" inputId="currency-japan" currency="JPY" locale="jp-JP"> </p-inputNumber>
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>
<p-inputNumber [(ngModel)]="value2" inputId="percent" prefix="%"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value3" inputId="expiry" prefix="Expires in " suffix=" days"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value4" prefix="↑ " inputId="temperature" suffix="℃" [min]="0" [max]="40"> </p-inputNumber>
Spinner buttons is 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>
<p-inputNumber [(ngModel)]="value2" mode="decimal" [showButtons]="true" inputId="minmax-buttons" [min]="0" [max]="100"> </p-inputNumber>
<p-inputNumber [(ngModel)]="value3" [showButtons]="true" buttonLayout="horizontal" inputId="horizontal" spinnerMode="horizontal" [step]="0.25"
decrementButtonClass="p-button-danger" incrementButtonClass="p-button-success" incrementButtonIcon="pi pi-plus" decrementButtonIcon="pi pi-minus" mode="currency" currency="EUR"></p-inputNumber>
Buttons can also placed vertically by setting buttonLayout as vertical.
<p-inputNumber [(ngModel)]="value1" [showButtons]="true" buttonLayout="vertical" spinnerMode="vertical" inputId="vertical" decrementButtonClass="p-button-secondary"
incrementButtonClass="p-button-secondary" incrementButtonIcon="pi pi-plus" decrementButtonIcon="pi pi-minus" ></p-inputNumber>
A floating label appears on top of the input field when focused.
<span class="p-float-label">
<p-inputNumber inputId="integeronly" [(ngModel)]="value1"> </p-inputNumber>
<label htmlFor="number-input">Number</label>
</span>
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"> </p-inputNumber>
When disabled is present, the element cannot be edited and focused.
<p-inputNumber inputId="integeronly" [disabled]="true" prefix="%" [(ngModel)]="value1"> </p-inputNumber>
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-inputnumber | Container element |
p-input-number-stacked | Container element with stacked buttons. |
p-input-number-horizontal | Container element with horizontal buttons. |
p-input-number-vertical | Container element with vertical buttons. |
p-input-number-input | Input element |
p-input-number-button | Input element |
p-input-number-button-up | Increment button |
p-input-number-button-down | Decrement button |
p-input-number-button-icon | Button icon |
Value to describe the component can either be provided via label tag combined with inputId prop or using aria-labelledby, aria-label 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"></p-inputNumber>
<span id="label_number">Number</span>
<p-inputNumber aria-labelledby="label_number"></p-inputNumber>
<p-inputNumber aria-label="Number"></p-inputNumber>
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. |
API defines helper props, events and others for the PrimeNG InputNumber module.
Name | Type | Default | Description |
---|---|---|---|
value | number | null | Value of the component. |
format | boolean | true | Whether to format the value. |
showButtons | boolean | false | Displays spinner buttons. |
buttonLayout | string | stacked | Layout of the buttons, valid values are "stacked" (default), "horizontal" and "vertical". |
incrementButtonClass | string | null | Style class of the increment button. |
decrementButtonClass | string | null | Style class of the decrement button. |
incrementButtonIcon | string | null | Style class of the increment button. |
decrementButtonIcon | string | null | Style class of the decrement button. |
locale | string | null | Locale to be used in formatting. |
localeMatcher | string | best fit | The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". See Locale Negotiation for details. |
mode | string | decimal | Defines the behavior of the component, valid values are "decimal" and "currency". |
prefix | string | null | Text to display before the value. |
suffix | string | decimal | Text to display after the value. |
currency | string | null | The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB. There is no default value; if the style is "currency", the currency property must be provided. |
currencyDisplay | string | symbol | How to display the currency in currency formatting. Possible values are "symbol" to use a localized currency symbol such as €, ü"code" to use the ISO currency code, "name" to use a localized currency name such as "dollar"; the default is "symbol". |
useGrouping | boolean | true | Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. |
minFractionDigits | number | null | The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information). |
maxFractionDigits | number | null | The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information). |
min | number | null | Mininum boundary value. |
max | number | null | Maximum boundary value. |
step | number | 1 | Step factor to increment/decrement the value. |
allowEmpty | boolean | true | Determines whether the input field is empty. |
style | string | null | Inline style of the component. |
styleClass | string | null | Style class of the component. |
inputId | string | null | Identifier of the focus input to match a label defined for the component. |
inputStyle | string | null | Inline style of the input field. |
inputStyleClass | string | null | Style class of the input field. |
placeholder | string | null | Advisory information to display on input. |
size | number | null | Size of the input field. |
maxlength | number | null | Maximum number of character allows in the input field. |
tabindex | number | null | Specifies tab order of the element. |
disabled | boolean | false | When present, it specifies that the element should be disabled. |
readonly | boolean | false | When present, it specifies that an input field is read-only. |
title | string | null | Title text of the input text. |
ariaLabel | string | null | Used to define a string that labels the input element. |
ariaRequired | boolean | false | Used to indicate that user input is required on an element before a form can be submitted. |
name | string | null | Name of the input field. |
autocomplete | string | null | Used to define a string that autocomplete attribute the current element. |
showClear | boolean | false | When enabled, a clear icon is displayed to clear the value. |
updateOn | string | change | Used to define when the model value updates. Possible values are 'change' and 'blur'. |
Name | Parameters | Description |
---|---|---|
onFocus | event: Browser event | Callback to invoke when input receives focus. |
onBlur | event: Browser event | Callback to invoke when input loses focus. |
onInput | event.originalEvent: Browser event event.value: New value | Callback to invoke when the value is entered. |
onClear | - | Callback to invoke when input field is cleared. |
Name | Parameters | Description |
---|---|---|
getFormatter | - | Returns Intl.NumberFormat object. |
Name | Parameters |
---|---|
clearicon | - |
incrementbuttonicon | - |
decrementbuttonicon | - |