Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mablay/metric-prefix
Format numbers using only significant digits and a metric prefix.
https://github.com/mablay/metric-prefix
format metric prefix si unit
Last synced: 8 days ago
JSON representation
Format numbers using only significant digits and a metric prefix.
- Host: GitHub
- URL: https://github.com/mablay/metric-prefix
- Owner: mablay
- Created: 2018-09-28T05:18:45.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-16T23:17:18.000Z (over 3 years ago)
- Last Synced: 2023-03-02T09:41:29.681Z (over 1 year ago)
- Topics: format, metric, prefix, si, unit
- Language: JavaScript
- Homepage: https://mablay.github.io/metric-prefix/
- Size: 65.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Metric Prefix
Format numbers using only significant digits and a [metric prefix](https://en.wikipedia.org/wiki/Metric_prefix).
prefix(1234567890)
// => '1.23 G'## Install
npm i metric-prefix
#### basic example
const { prefix } = require('metric-prefix')
prefix(0.123456789)
// => '123 m'#### with options
prefix(123456, { unit: 'Hz' })
// => '123 kHz'prefix(123456, { delimiter: '' })
// => '123k'prefix(123.456, { precision: 4 })
// => '123.4'#### cure bulky options with factory pattern
const prefix = require('metric-prefix')({
unit: 'W',
precision: 4,
delimiter: ''
})prefix(0.0321) // => '32.10mW'
#### supported value types
Due to [big.js](https://github.com/MikeMcl/big.js)
being used internally, all compatible data types can
be used.prefix("100000000000000000000000000")
// => 100 Yprefix(Big(1e26))
// => 100 Y## Additional Notes
#### Range
Numbers from 1e-33 to 1e35 can be prefixed. Including non standard prefixes *X*, *W*, *V*. Beyond that a `?` will replace the prefix.#### Rounding
Prefix *floors* positive and *ceils* negative numbers.#### Floating Point Arithmetic
In JavaScripts you might encounter situations like0.3 - 0.1 = 0.19999999999999998
that lead to
prefix(0.3 - 0.1) // => '199 m'
and can be fixed with
prefix(Big(0.3).minus(0.1)) // => '200 m'