Installations
npm install react-currency-format
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
14.16.0
NPM Version
6.14.11
Score
94.3
Supply Chain
95.2
Quality
74.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.17%)
HTML (0.54%)
CSS (0.29%)
Developer
mohitgupta8888
Download Statistics
Total Downloads
5,148,444
Last Day
5,375
Last Week
30,731
Last Month
113,018
Last Year
1,341,526
GitHub Statistics
118 Stars
105 Commits
29 Forks
5 Watching
13 Branches
12 Contributors
Bundle Size
14.38 kB
Minified
4.95 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.1.0
Package Id
react-currency-format@1.1.0
Unpacked Size
155.25 kB
Size
33.11 kB
File Count
28
NPM Version
6.14.11
Node Version
14.16.0
Total Downloads
Cumulative downloads
Total Downloads
5,148,444
Last day
-16.4%
5,375
Compared to previous day
Last week
3.4%
30,731
Compared to previous week
Last month
12.1%
113,018
Compared to previous month
Last year
24.1%
1,341,526
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
40
react-currency-format
React component to format number in an input or as a text
Features
- Prefix, suffix and thousand separator.
- Custom format pattern.
- Masking.
- Custom formatting handler.
- Formatting a input or a simple text.
Install
Through npm
npm install react-currency-format --save
Or get compiled development and production version from ./dist
Usage
ES6
1import CurrencyFormat from 'react-currency-format';
ES5
1const CurrencyFormat = require('react-currency-format');
Typescript
1import * as CurrencyFormat from 'react-currency-format';
Props
Props | Options | Default | Description |
---|---|---|---|
thousandSeparator | mixed: single character string or boolean true (true is default to ,) | none | Add thousand separators on number |
thousandSpacing | String, One of ['2', '2s', '3', '4'] | '3' | Add thousand group spacing on number. Default: '2' will format like 1,23,45,67,89 __ '3' will format like 1,234,567,981 __ '2s' will format like 1,23,45,67,981 __ '4' will format like 1,2345,6789 |
decimalSeparator | single character string | . | Support decimal point on a number |
decimalScale | number | none | If defined it limits to given decimal scale |
fixedDecimalScale | boolean | false | If true it add 0s to match given decimalScale |
allowNegative | boolean | true | allow negative numbers (Only when format option is not provided) |
prefix | String (ex : $) | none | Add a prefix before the number |
suffix | String (ex : /-) | none | Add a prefix after the number |
value | Number or String | null | Value to the number format. It can be a float number, or formatted string. If value is string representation of number (unformatted), isNumericString props should be passed as true. |
isNumericString | boolean | false | If value is passed as string representation of numbers (unformatted) then this should be passed as true |
displayType | String: text / input | input | If input it renders a input element where formatting happens as you input characters. If text it renders it as a normal text in a span formatting the given value |
type | One of ['text', 'tel'] | text | Input type attribute |
format | String : Hash based ex (#### #### #### ####) Or Function | none | If format given as hash string allow number input inplace of hash. If format given as function, component calls the function with unformatted number and expects formatted number. |
removeFormatting | (formattedValue) => numericString | none | If you are providing custom format method and it add numbers as format you will need to add custom removeFormatting logic |
mask | String (ex : _) | none | If mask defined, component will show non entered placed with masked value. |
customInput | Component Reference | input | This allow supporting custom inputs with number format. |
onValueChange | (values) => {} | none | onValueChange handler accepts values object |
isAllowed | (values) => true or false | none | A checker function to check if input value is valid or not |
renderText | (formattedValue) => React Element | null | A renderText method useful if you want to render formattedValue in different element other than span. |
Other than this it accepts all the props which can be given to a input or span based on displayType you selected.
values object
values object is on following format
1{ 2 formattedValue: '$23,234,235.56', //value after applying formatting 3 value: '23234235.56', //non formatted value as numeric string 23234235.56, if your are setting this value to state make sure to pass isNumericString prop to true 4 floatValue: 23234235.56 //floating point representation. For big numbers it can have exponential syntax 5}
Its recommended to use formattedValue / value / floatValue based on the initial state (it should be same as the initial state format) which you are passing as value prop. If you are saving the value
key on state make sure to pass isNumericString prop to true.
Notes and quirks
-
Value can be passed as string or number, but if it is passed as string it should be either formatted value or if it is a numeric string, you have to set isNumericString props to true.
-
Value as prop will be rounded to given decimal scale if format option is not provided.
-
If you want to block floating number set decimalScale to 0.
-
Use type as tel when you are providing format prop. This will change the mobile keyboard layout to have only numbers. In other case use type as text, so user can type decimal separator.
-
onChange no longer gets values object. You need to use onValueChange instead. onChange/onFocus/onBlur and other input events will be directly passed to the input.
-
Its recommended to use formattedValue / value / floatValue based on the initial state (it should be same as the initial state format) which you are passing as value prop. If you are saving the
value
key on state make sure to pass isNumericString prop to true.
Examples
Prefix and thousand separator : Format currency as text
1var CurrencyFormat = require('react-currency-format'); 2 3<CurrencyFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} />
Output : $2,456,981
Custom renderText method
1var CurrencyFormat = require('react-currency-format'); 2 3<CurrencyFormat value={2456981} displayType={'text'} thousandSeparator={true} prefix={'$'} renderText={value => <div>{value}</div>} />
Output : <div> $2,456,981 </div>
Format with pattern : Format credit card as text
1<CurrencyFormat value={4111111111111111} displayType={'text'} format="#### #### #### ####" />
Output : 4111 1111 1111 1111
Prefix and thousand separator : Format currency in input
1<CurrencyFormat thousandSeparator={true} prefix={'$'} />
Maintaining change value on state
1<CurrencyFormat value={this.state.profit} thousandSeparator={true} prefix={'$'} onValueChange={(values) => { 2 const {formattedValue, value} = values; 3 // formattedValue = $2,223 4 // value ie, 2223 5 this.setState({profit: formattedValue}) 6 }}/>
Format with pattern : Format credit card in an input
1<CurrencyFormat format="#### #### #### ####" />
Format with mask : Format credit card in an input
1<CurrencyFormat format="#### #### #### ####" mask="_"/>
Format with mask as array
Mask can also be a array of string. Each item corresponds to the same index #.
1<CurrencyFormat format="##/##" placeholder="MM/YY" mask={['M', 'M', 'Y', 'Y']}/>
Custom format method : Format credit card expiry time
1function limit(val, max) { 2 if (val.length === 1 && val[0] > max[0]) { 3 val = '0' + val; 4 } 5 6 if (val.length === 2) { 7 if (Number(val) === 0) { 8 val = '01'; 9 10 //this can happen when user paste number 11 } else if (val > max) { 12 val = max; 13 } 14 } 15 16 return val; 17} 18 19function cardExpiry(val) { 20 let month = limit(val.substring(0, 2), '12'); 21 let year = val.substring(2, 4); 22 23 return month + (year.length ? '/' + year : ''); 24} 25 26<CurrencyFormat format={cardExpiry}/>
Format phone number
1<CurrencyFormat format="+1 (###) ###-####" mask="_"/>
Custom Inputs
You can easily extend your custom input with number format. But custom input should have all input props.
1 import TextField from 'material-ui/TextField';
1 <CurrencyFormat customInput={TextField} format="#### #### #### ####"/>
Passing custom input props All custom input props and number input props are passed together.
1 <CurrencyFormat hintText="Some placeholder" value={this.state.card} customInput={TextField} format="#### #### #### ####"/>
Live Demo
http://codepen.io/mohitgupta8888/pen/bpKNMa
Show your support
Migrate v2 to v3
Updates
For regular updates follow me on _syadav
Major updates
v3.0.0
- onChange no longer gets values object. You need to use onValueChange instead. This is done because formatted value may change on onBlur event. calling onChange on onBlur doesn't feel right.
- decimalPrecision is changed to decimalScale. Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number.
- decimalScale by default will not add 0s to match provided decimalScale value like decimalPrecision. You have to set fixedDecimalScale to true.
- mask can be now array of string in which case mask at specific index will be mapped with the # of the pattern.
- Value can be passed as string or number, but if it is passed as string it should be either formatted value or if it is a numeric string, you have to set isNumericString props to true.
- Added support for numbers in prefix / suffix / pattern.
- Fixed caret position and formatting issues.
- Lot of bugs and stability fixes (See release notes)
v2.0.0
- Added isAllowed prop to validate custom input and reject input based on it.
- onChange api been changed. Now it receives values object as second parameter.
- decimalSeparator no longer support boolean values
- thousandSeparator accepts only true as boolean (which defaults to ,) or thousandSeparator string
- decimalPrecision only accepts number now
- Value can be passed as string or number but if it is passed as string you should maintain the same decimal separator on the string what you provided as decimalSeparator prop.
- Added back the type prop for the input type attribute (Only text or tel is supported)
- Enforce cursor to be between prefix and suffix in focus, click or arrow navigation.
- Lot of bugs and stability fixes (See release notes)
Development
- Download the zip
npm install
npm start
to run example servernpm run test
to test changesnpm run bundle
to bundle files
Testing
Test cases are written in jasmine and run by karma
Test file : /test/test_input.js
To run test : npm run test
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: MIT-LICENSE.txt:0
- Info: FSF or OSI recognized license: MIT License: MIT-LICENSE.txt:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/28 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'react-currency-format'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
69 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-28xh-wpgr-7fm8
- Warn: Project is vulnerable to: GHSA-q75g-2496-mxpp
- Warn: Project is vulnerable to: GHSA-6fx8-h7jm-663j
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-cf66-xwfp-gvc4
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-currency-format
react-number-format
React component to format number in an input or as a text.
@types/react-currency-format
TypeScript definitions for react-currency-format
react-native-format-currency
A lightweight international currency formatter for React Native & Expo (iOS and Android).
react-native-currency-format
TODO