Top Related Projects
Input masking component for React. Made with attention to UX.
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Format input text content when you are typing...
A zero-config, drop-in animation utility that adds smooth transitions to your web app. You can use it with React, Vue, or any other JavaScript application.
Quick Overview
React Number Format is a lightweight React component for formatting and manipulating numbers in input fields. It provides a flexible way to format numbers as currency, percentages, or custom formats while maintaining a clean and user-friendly interface.
Pros
- Easy integration with React applications
- Highly customizable formatting options
- Supports both controlled and uncontrolled components
- Handles decimal precision and thousand separators automatically
Cons
- Limited to React applications only
- May require additional configuration for complex formatting needs
- Performance might be affected for large numbers of inputs on a single page
- Learning curve for advanced customizations
Code Examples
Basic usage:
import NumberFormat from 'react-number-format';
function MyComponent() {
return (
<NumberFormat
value={1000000}
displayType={'text'}
thousandSeparator={true}
prefix={'$'}
/>
);
}
This example displays the number 1000000 as formatted text: $1,000,000.
Custom format:
<NumberFormat
value={5103.22}
displayType={'text'}
thousandSeparator={true}
prefix={'€'}
decimalScale={2}
fixedDecimalScale={true}
/>
This example formats the number with a Euro symbol, thousand separators, and fixed two decimal places: €5,103.22.
Input with maximum value:
<NumberFormat
value={value}
onValueChange={(values) => {
const {formattedValue, value} = values;
setValue(value);
}}
isAllowed={(values) => {
const {floatValue} = values;
return floatValue <= 1000;
}}
/>
This example creates an input field that only allows values up to 1000.
Getting Started
-
Install the package:
npm install react-number-format
-
Import and use in your React component:
import NumberFormat from 'react-number-format'; function MyComponent() { return ( <NumberFormat value={1234.56} displayType={'text'} thousandSeparator={true} prefix={'$'} /> ); }
-
Customize the formatting options as needed, such as
decimalScale
,fixedDecimalScale
,suffix
, etc. -
For input fields, use the
onValueChange
prop to handle value changes and update your component's state accordingly.
Competitor Comparisons
Input masking component for React. Made with attention to UX.
Pros of react-input-mask
- More flexible for various input types beyond numbers
- Supports complex mask patterns with custom placeholders
- Allows for dynamic mask changes based on user input
Cons of react-input-mask
- Less specialized for number formatting
- May require more configuration for simple number inputs
- Slightly larger bundle size
Code Comparison
react-input-mask:
import InputMask from 'react-input-mask';
<InputMask
mask="+1 (999) 999-9999"
value={phone}
onChange={handleChange}
/>
react-number-format:
import NumberFormat from 'react-number-format';
<NumberFormat
format="+1 (###) ###-####"
value={phone}
onValueChange={handleChange}
/>
Both libraries offer similar functionality for masked inputs, but react-input-mask provides more flexibility for various input types, while react-number-format is more specialized for number formatting. react-input-mask allows for complex mask patterns and dynamic masks, which can be beneficial for diverse input requirements. However, for simple number formatting tasks, react-number-format may be more straightforward to use and configure. The code comparison shows that both libraries have similar syntax, with slight differences in prop names and handling of changes.
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Pros of text-mask
- Supports multiple input types (text, number, etc.) and platforms (React, Vue, Angular, vanilla JS)
- Offers more flexibility in mask creation with custom functions
- Provides addons for specific use cases (e.g., auto-corrected date pipe)
Cons of text-mask
- Larger bundle size due to support for multiple platforms
- May require more setup and configuration for specific use cases
- Less focused on number formatting compared to react-number-format
Code Comparison
text-mask:
import MaskedInput from 'react-text-mask'
<MaskedInput
mask={[/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/]}
placeholder="Enter a date"
/>
react-number-format:
import NumberFormat from 'react-number-format'
<NumberFormat
format="##/##/####"
placeholder="Enter a date"
mask={['D', 'D', 'M', 'M', 'Y', 'Y', 'Y', 'Y']}
/>
Both libraries offer similar functionality for basic masking, but text-mask provides more flexibility in mask definition, while react-number-format focuses on number formatting with built-in features for currency and percentages.
Format input text content when you are typing...
Pros of Cleave.js
- Framework-agnostic, can be used with any JavaScript project
- Supports more input types (credit cards, phone numbers, date, time)
- Smaller bundle size (11.5kB vs 17.6kB gzipped)
Cons of Cleave.js
- Less focused on number formatting compared to React Number Format
- Doesn't provide as many number-specific features (e.g., prefix, suffix, custom formatting)
- Not specifically optimized for React applications
Code Comparison
React Number Format:
import NumberFormat from 'react-number-format';
<NumberFormat
value={2456981}
displayType={'text'}
thousandSeparator={true}
prefix={'$'}
/>
Cleave.js:
import Cleave from 'cleave.js/react';
<Cleave
options={{
numeral: true,
numeralThousandsGroupStyle: 'thousand',
prefix: '$'
}}
value="2456981"
/>
Both libraries offer similar functionality for number formatting, but React Number Format provides more specialized features for number handling, while Cleave.js offers a broader range of input formatting options. The choice between them depends on the specific project requirements and whether the application is React-based or needs to support multiple frameworks.
A zero-config, drop-in animation utility that adds smooth transitions to your web app. You can use it with React, Vue, or any other JavaScript application.
Pros of Auto Animate
- Framework-agnostic, works with various JavaScript frameworks and vanilla JS
- Provides automatic animations for adding, removing, and moving elements
- Simple API with minimal configuration required
Cons of Auto Animate
- Limited to animating DOM elements, not specialized for number formatting
- May require additional setup for complex animations or custom easing functions
- Doesn't provide specific number formatting features like decimal places or currency symbols
Code Comparison
Auto Animate:
import { autoAnimate } from '@formkit/auto-animate'
useEffect(() => {
parent && autoAnimate(parent)
}, [parent])
React Number Format:
import NumberFormat from 'react-number-format';
<NumberFormat
value={2456981}
displayType={'text'}
thousandSeparator={true}
prefix={'$'}
/>
Summary
Auto Animate is a versatile animation library for DOM elements, while React Number Format is specifically designed for formatting numbers in React applications. Auto Animate offers ease of use across different frameworks, but lacks specialized number formatting features. React Number Format provides robust number formatting options but is limited to React projects. The choice between the two depends on the specific needs of the project, whether it requires general animations or specialized number formatting.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-number-format
React Number Format is an input-formatter library with a sophisticated and light weight caret engine. It ensures that a user can only enter text that meets specific numeric or string patterns, and formats the input value for display.
Features
- Prefix, suffix and thousands separator.
- Input Masking.
- Format number in an input or format as a simple text.
- Custom pattern formatting.
- Custom formatting handler.
- Fully customizable
Demos
See the many DEMO sections in the documentation.
Install
Using npm
npm install react-number-format
Using yarn
yarn add react-number-format
Documentation
Read the full documentation here https://s-yadav.github.io/react-number-format/docs/intro
ES6
Numeric Format
import { NumericFormat } from 'react-number-format';
NumericFormat Props: https://s-yadav.github.io/react-number-format/docs/numeric_format
Pattern Format
import { PatternFormat } from 'react-number-format';
PatternFormat Props: https://s-yadav.github.io/react-number-format/docs/pattern_format
Migrate from v4 to v5
https://s-yadav.github.io/react-number-format/docs/migration
v4 doc
Development
- Clone the repository or download the zip
npm i -g yarn
to download Yarnyarn
to install dependenciesyarn start
to run example server (http://localhost:8084/)yarn test
to test changesyarn build
to bundle files
Testing
Test cases are written in jasmine and run by karma
Test files : /test/**/*.spec.js
To run test : yarn test
Top Related Projects
Input masking component for React. Made with attention to UX.
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Format input text content when you are typing...
A zero-config, drop-in animation utility that adds smooth transitions to your web app. You can use it with React, Vue, or any other JavaScript application.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot