https://github.com/exodusmovement/format-num
JavaScript component to format numbers to strings.
https://github.com/exodusmovement/format-num
Last synced: 8 months ago
JSON representation
JavaScript component to format numbers to strings.
- Host: GitHub
- URL: https://github.com/exodusmovement/format-num
- Owner: ExodusMovement
- License: mit
- Archived: true
- Created: 2016-03-17T15:25:48.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-08-27T12:18:48.000Z (almost 2 years ago)
- Last Synced: 2025-02-10T05:11:35.564Z (over 1 year ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 15
- Watchers: 33
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
format-num
==========
[](https://www.npmjs.org/package/format-num)
[](https://travis-ci.org/ExodusMovement/format-num)
[](http://standardjs.com/)
`format-num` is a JavaScript component to format numbers to strings. Used in [Exodus Ethereum Wallet](http://www.exodus.io/).
Install
-------
npm install --save format-num
Notes
-----
- Must have a JavaScript environment with `Object.assign` and `Intl.NumberFormat`.
In Node.js, this is at least v4. You can install in older environments, you'll just
need to polyfill.
Usage
-----
### formatNum
**Signature:** `formatNum(value, [options])`
**Parameters:**
- `value`: Value to convert. Will pass through [parse-num](https://github.com/ExodusMovement/parse-num) first.
Will coerce anything to a number.
- `options`: *optional* `object` parameter to specify [options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat).
Appending `Digits` is not necessary. You can also shorten `maximum` to `max` and `minimum` to `min`. Adds one more option `nanZero`, which when the number is
`NaN`, if it should be coerced to `0` - defaults to `true` i.e. `NaN => '0'`.
**Returns:**
The `string` representation of the number.
**Example:**
```js
const formatNum = require('format-num')
console.log(formatNum(10000000.15)) // => 10,000,000.15
console.log(formatNum('10000000.15')) // => 10,000,000.15
console.log(formatNum(0.0000000000044535, { maxSignificant: 2 })) // => 0.0000000000045
console.log(formatNum(0, { minFraction: 2, maxFraction: 2 })) // => 0.00
console.log(formatNum(null)) // => 0
console.log(formatNum(null, { nanZero: false })) // => NaN
```
Related
-------
- [parse-num](https://github.com/ExodusMovement/parse-num): Parse anything into a number. A dependency
of this library.
- [number-unit](https://github.com/ExodusMovement/number-unit): Numbers with units. Easily convert numbers to from different units.
License
-------
MIT