React component INPUT currency and numbers.
- Automatic insertion of thousand separators
- Support for decimal places
- Currency formatting
- Customizable decimal and thousand separators
- Handles negative numbers
- Supports abbreviations (K, M, B, T) (e.g., 1.2K for 1,200 or 3.5M for 3,500,000 etc.)
- Easy to use with React
- Lightweight and fast
You can see a live demo of the component here.
npm install react-number-inputimport React, { useState } from 'react';
import NumberInput from 'react-number-input';
const App = () => {
const [value, setValue] = useState('1e6');
return (
<div>
<h1>React Number Input Example</h1>
<NumberInput
value={value}
onValueChange={(value: string, num: number, formatted: string) => setValue(value)}
decimalLimit={2}
thousandSeparator={','}
decimalSeparator={'.'}
allowNegative={true}
placeholder="Enter a number"
step={1}
/>
<p>Current Value: {value}</p>
</div>
);
};
export default App;| Name | Type | Default | Description |
|---|---|---|---|
value |
string | number |
"" |
The current value of the input field. |
onValueChange |
function |
undefined |
Callback function called when the value changes. Receives the raw numeric value and formatted string. |
decimalLimit |
number |
undefined |
Maximum number of digits allowed after the decimal point. |
thousandSeparator |
string |
"," |
|
decimalSeparator |
string |
". " |
Character to use as the decimal separator. |
allowNegative |
boolean |
false |
|
placeholder |
string |
"" |
|
min |
number |
undefined |
Minimum value allowed. |
max |
number |
undefined |
Maximum value allowed. |
step |
number |
1 |
Increment/decrement step value. |
| Other standard input props | All other standard HTML input props are supported. |
Handle changes to the value.
onValueChange = (value, num, formated) => void;value: The raw numeric value (e.g.,1234.56).num: The numeric value as a number type (e.g., 1234.56).formated: The formatted string value (e.g.,"1,234.56").
Maximum number of digits allowed after the decimal point.
decimalLimit = number;number: Maximum digits after the decimal point (e.g.,2for two decimal places). Examples:- If
decimalLimitis set to2, the input will allow values like1234.56but not1234.567. - If
decimalLimitis set to0, only whole numbers will be allowed.
Character to use as the thousand separator.
thousandSeparator = string | boolean;string: Character to use as the thousand separator (e.g.,","for comma,"."for dot).boolean: If set totrue, defaults to comma (,) as the thousand separator. Examples:- If
thousandSeparatoris set to",", the input will format1234567as1,234,567. - If
thousandSeparatoris set to".", the input will format1234567as1.234.567.
Character to use as the decimal separator.
decimalSeparator = string;string: Character to use as the decimal separator (e.g.,"."for dot,","for comma). Examples:- If
decimalSeparatoris set to".", the input will format1234.56as1,234.56. - If
decimalSeparatoris set to",", the input will format1234,56as1.234,56.
Whether to allow negative numbers.
allowNegative = boolean;boolean: If set totrue, negative numbers are allowed; iffalse, only positive numbers are allowed. Examples:- If
allowNegativeis set totrue, the input will accept values like-1234.56. - If
allowNegativeis set tofalse, the input will only accept values like1234.56.
Always support abbreviations for large numbers. Examples:
1.2Kfor1,2003.5Mfor3,500,0007.8Bfor7,800,000,0009.1Tfor9,100,000,000,000abbreviationsis always enabled and does not require a separate prop.
MIT License © 2024 AnnhDev