Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
number-currency-format
Advanced tools
Zero-dependency, cross-browser, lightweight and flexible utility for number/price formatting that you control.
npm i number-currency-format
or
yarn add number-currency-format
Format
const { format } = require('number-currency-format');
format(1999.99);
// '1,999.99'
currency
format(1999.99, {
currency: '€'
});
// '1,999.99 €'
format(1999.99, {
currency: '$',
spacing: false,
currencyPosition: 'LEFT'
})
//'$1,999.99'
Unformat
const { unformat } = require('number-currency-format');
unformat('1.999,99');
// 1999.99
const { unformat } = require('number-currency-format');
unformat('$ 199.990,05');
// 199990.05
To see all supported usecases and example usages, run npm test
or yarn test
.
format
format(number: number, options?: FormattingOptions)
Formats number given defined formatting options
number
- Number to be formattedFormattingOptions
- Formatting options (optional)FormattingOptions
string
- Currency symbol to be printed next to the formatted number. By default: nonestring
- Symbol separating thousands. By default: ,
string
- Symbol separating decimals. By default: .
number
- Number of decimal digits. By default: 2
string
- ALWAYS
, IF_NEEDED
, AS_IS
or NEVER
. IF_NEEDED
does not show the decimal if it is 0 (and if it is different than 0, shows exactly {decimalsDigits}
decimal digits). AS_IS preserves the decimal depth of the source number. By default: ALWAYS
string
- LEFT
or RIGHT
. By default: RIGHT
boolean
- Spacing between currency and price. By default: true
boolean
- Use arithmetical rounding (always half-up) instead of tie break rounding. By default: false
(so: the rounding will include tie-breaking)All configuration is optional.
unformat
unformat(text: string, options?: FormattingOptions)
Reads the number out of a string that looks like a price.
text
- Text to be unformattedFormattingOptions
- Formatting options (optional). Currently only decimalSeparator
is supported by the unformatter as the rest of the options don't really make sense and the unformatter can extract the price out of the text anyway.This works in every modern browser as well as on the server. This module does not contain and will never contain any dependency at all. This module does not use any 3rd party bundler, transpiler, compiler or test runner. Vanilla all the way 😎.
intl
or other packages?Intl does not offer full flexibility around price formatting and has an overhead of loading all locale files. Additionally it requires polyfill for old browsers and headless testing. This library is bare to the bone and does not contain all possible locales definition and gives you all possible formatting options to support all known price formats.
FAQs
Dependency-less price formatting
We found that number-currency-format demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.