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 { rootProps, state } = useInputNumber({
    defaultValue: 0
});
 
return <input {...rootProps} />;

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

Features#

  • rootProps returns spread-ready attributes including role="spinbutton", ARIA value properties, and all event handlers
  • state.value and state.formattedValue track the raw numeric and display-formatted values
  • increment, decrement, stepUp, and stepDown methods for programmatic and UI-driven value changes
  • maxBoundry() and minBoundry() helpers for checking boundary state
  • Auto-repeating spin on pointer hold with accelerating intervals

Behavior#

Default Value#

Set defaultValue for uncontrolled initial value.

const inputnumber = useInputNumber({ defaultValue: 42 });

Controlled#

Pass value with onValueChange for controlled usage.

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

Min and Max#

Set min and max to enforce boundaries. Values are clamped on blur and during spin.

const inputnumber = useInputNumber({ min: 0, max: 100 });

Step#

Set step to control the increment per spin operation. Defaults to 1.

const inputnumber = useInputNumber({ step: 5 });

Locale and Grouping#

Set locale for locale-aware formatting and useGrouping to toggle grouping separators.

const inputnumber = useInputNumber({
    locale: 'de-DE',
    useGrouping: true
});

Currency Mode#

Set mode to 'currency' and specify the currency code for monetary formatting.

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

Fraction Digits#

Control decimal precision with minFractionDigits and maxFractionDigits.

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

Prefix and Suffix#

Add custom text around the number with prefix and suffix.

const inputnumber = useInputNumber({
    prefix: '$',
    suffix: ' USD'
});

Highlight on Focus#

Set highlightOnFocus to automatically select the input text when the field receives focus.

const inputnumber = useInputNumber({ highlightOnFocus: true });

Custom Styling with Data Attributes#

[data-scope='inputnumber'][data-part='root'] { ... }

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"ceil" | "floor" | "expand" | "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.
onChange(event: useInputNumberValueChangeEvent) => void—
Callback to invoke when value changes.
onValueChange(event: useInputNumberValueChangeEvent) => void—
Callback to invoke after validation check and value change.

Accessibility#

See InputNumber Primitive for WAI-ARIA compliance details and keyboard support.