price-like-humans
v0.8.0
Published
JS tools for formatting price or numbers to human likes format.
Downloads
14
Maintainers
Readme
Price like Humans
JS kit for formatting price or numbers to human likes format. Also kit will be useful for crypto-currency with 7+ numbers after a delimiter
- Written with TypeScript
- Corrected with Prettier
- Tested with Jest
- Bundled with Rollup
⚠️ Breaking changes from 0.6.0 to 0.7.0 see the changelog
Spoiler: changed arguments in formattedPrice
Table of contents
Install
NPM users:
npm i --save-dev price-like-humans
Yarn users:
yarn add price-like-humans -D
Methods
| Methods | Returns | Description |
| -------------------------------- | -------- | --------------------------------------------------------------------------- |
| formattedPrice(value, options)
| string
| Formatting incoming numbers to humans like price with user locale delimiter |
| exponentFormatter(value)
| string
| Formatting exponential numbers to human likes numbers. Exponent free |
formattedPrice
| Argument | Required | Argument type | Description |
| -------- | ------------ | ---------------- | ---------------------------------------------------------------------------------------------------------------- |
| value | *required | number, string
| Incoming numbers which will be formatted (exponential friendly) |
| options | optional | object
| Settings list, see formattedPrice options |
exponentformatter
| Argument | Required | Argument type | Description |
| -------- | ------------ | ---------------- | ---------------------------------------- |
| value | *required | number, string
| Incoming exponential numbers which will be formatted |
formattedPrice options
| Argument | Argument type | Description |
| --------- | ------------- | ------------------------------------------------------------- |
| delimiter | string
| Delimiter symbol. Number which split decimal. Can be replaced |
| separator | string
| Symbol which separates grouped number. Can be replaced |
| lang | string
| You can set locale option. Using user locale by default |
⚠️ Warning: When works in Nodejs environment, intl.NumberFormat contains 'en-US' locale only, so use the separator
with delimiter
when the code needs to run on a server-side.
Usage
NodeJS
const priceLikeHumans = require('price-like-humans');
ES6
import priceLikeHumans from 'price-like-humans';
// or methods only
import { formattedPrice, exponentFormatter } from 'price-like-humans';
Examples
formattedPrice
examples
Without separator arguments (putted your local separator)
formattedPrice(12345.6789);
//> "12,345.678,9" // EU Locale
//> "12 345.678 9" // RU Locale
Using with options
formattedPrice(12345.6789, { delimiter: ',' });
//> "12.345,678.9" // EN Locale
//> "12 345,678 9" // RU Locale
formattedPrice(12345.6789, { separator: '.' });
//> "12.345,678.9" // EN Locale
//> "12.345,678.9" // RU Locale
formattedPrice(12345.6789, { delimiter: '.', separator: ',' });
//> "12,345.678,9"
formattedPrice(12345.6789, { lang: 'ru' });
//> "12 345,678 9"
formattedPrice(12345.6789, { lang: 'en' });
//> "12,345.678,9"
formattedPrice(1e-7, { lang: 'en' });
//> "0.000,000,1"
exponentFormatter
examples
exponentFormatter(1e-7);
//> "0.0000001"
Changelog
License
Price Like Humans is MIT licensed.