Introducing PrimeReact v11-alpha 🎉Discover Now

useInputNumber

Hook that manages numeric input with formatting, locale support, and spin button behavior.

basic-demo

Usage#

import { useInputNumber } from '@primereact/headless/inputnumber';
const { inputProps, state } = useInputNumber({
    defaultValue: 0
});
 
return <input {...inputProps} value={state.formattedValue} />;

useInputNumber handles numeric parsing, formatting, keyboard-driven increment/decrement, and boundary enforcement. See Primitive for a component-based API.

Features#

  • Numeric value state — tracks raw value and a locale-formatted display string in state.value / state.formattedValue
  • Spin button semantics — applies role="spinbutton" and ARIA value properties on the input
  • Keyboard and pointer stepping — increment, decrement, stepUp, stepDown plus auto-repeat on pointer hold
  • Locale-aware formatting — decimal, currency, percent, and grouping configuration via Intl.NumberFormat
  • Boundary enforcement — min/max clamp on blur and during spin, with minBoundry() / maxBoundry() helpers
  • Affix support — optional prefix and suffix text parsed back out of the displayed value

Working with callbacks#

Controlled value#

Pass value and onValueChange to drive the number from outside state.

const [value, setValue] = React.useState(0);
 
useInputNumber({
    value,
    onValueChange: (e) => setValue(e.value)
});

Currency formatting#

Combine mode, currency, and locale to render monetary values without custom parsers.

useInputNumber({
    mode: 'currency',
    currency: 'USD',
    locale: 'en-US'
});

Clamped range with custom step#

Use min, max, and step together when the field represents a bounded quantity like a rating or percentage.

useInputNumber({
    min: 0,
    max: 100,
    step: 5
});

Fraction precision#

Control decimal precision with minFractionDigits and maxFractionDigits — useful for financial or scientific fields.

useInputNumber({
    minFractionDigits: 2,
    maxFractionDigits: 4
});

Highlight on focus#

Set highlightOnFocus so the input selects its text on focus, letting users overwrite rather than edit.

useInputNumber({ highlightOnFocus: true });

Styling with data attributes#

Each part exposes data-scope="inputnumber" and a data-part identifier (root, input). The root reflects state with data-disabled, data-invalid, and data-readonly so descendant selectors can react.

[data-scope='inputnumber'][data-part='input'] {
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 0.25rem;
}
 
[data-scope='inputnumber'][data-part='input']:focus {
    border-color: var(--p-primary-color);
    outline: none;
}
 
[data-scope='inputnumber'][data-part='root'][data-disabled] [data-part='input'] {
    opacity: 0.5;
    cursor: not-allowed;
}

API#

useInputNumber#

NameTypeDefault
valuenumber—
Specifies whether a inputnumber should be checked or not.
defaultValuenumber—
Specifies whether a inputnumber should be checked or not.
namestring—
The name attribute for the element, typically used in form submissions.
formatbooleantrue
Whether to format the value.
localestring—
Locale to be used in formatting.
localeMatcher"lookup" | "best fit"'best fit'
The locale matching algorithm to use. Possible values are 'lookup' and 'best fit'; the default is 'best fit'. See [Locale Negotation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_negotiation) for details.
mode"decimal" | "currency"decimal
Defines the behavior of the component.
prefixstring—
Text to display before the value.
suffixstring—
Text to display after the value.
currencystring—
The currency to use in currency formatting. Possible values are the [ISO 4217 currency codes](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency), 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"symbol" | "code" | "name" | "narrowSymbol"—
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'.
useGroupingbooleantrue
Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators.
minFractionDigitsnumber—
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](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency) list (2 if the list doesn't provide that information).
maxFractionDigitsnumber—
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](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency) list (2 if the list doesn't provide that information).
roundingMode"expand" | "ceil" | "floor" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven"—
How decimals should be rounded. [further information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingmode).
minnumber—
Minimum boundary value.
maxnumber—
Maximum boundary value.
stepnumber1
Step factor to increment/decrement the value.
allowEmptybooleantrue
Determines whether the input field is empty.
highlightOnFocusbooleanfalse
Highlights automatically the input value.
invalidbooleanfalse
When present, it specifies that the component should have invalid state style.
disabledbooleanfalse
When present, it specifies that the component should be disabled.
readOnlybooleanfalse
When present, it specifies that an input field is read-only.
onChange(event: useInputNumberValueChangeEvent) => void—
Callback to invoke when value changes.
onValueChange(event: useInputNumberValueChangeEvent) => void—
Callback to invoke after validation check and value change.

Accessibility#

Up/Down arrows increment or decrement, Shift+arrows step by 10, and type-ahead accepts numeric input. See Primitive for full WAI-ARIA compliance details.