https://github.com/lakca/decent-unit
Decent conversion of measurement units.
https://github.com/lakca/decent-unit
conversion decent unit
Last synced: about 1 year ago
JSON representation
Decent conversion of measurement units.
- Host: GitHub
- URL: https://github.com/lakca/decent-unit
- Owner: lakca
- License: mit
- Created: 2020-08-19T04:33:28.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-04T08:44:47.000Z (almost 6 years ago)
- Last Synced: 2025-03-18T04:05:05.477Z (about 1 year ago)
- Topics: conversion, decent, unit
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
README
# decent-unit
Decent conversion of measurement units.
[](https://travis-ci.org/lakca/decent-unit)
[](https://codecov.io/gh/lakca/decent-unit)
## Usage
```js
const decentUnit = require('@lakca/decent-unit')
```
## Example
```js
// decent result: { value: 2, unit: 'G' }
decentUnit(2 * 1024 ** 3)
// decent result: { value: 2, unit: 'E' }
decentUnit(2 * 1024 ** 3, { fromUnit: 'G' })
// decent result { value: 2, unit: 'E' }
decentUnit(2 * 1000 ** 3, { fromUnit: 'G', radix: 1000 })
// decent result { value: 1, unit: 'G' }
decentUnit(20000, { fromUnit: '', radix: [100, 20, 10, 2], units: ['', 'K', 'M', 'G', 'T'] })
// expected result { value: 2048, unit: 'P' }
decentUnit(2 * 1024 ** 3, { fromUnit: 'G', toUnit: 'P' })
```
More in [test](test/main.js).
## API
### `decentUnit(num: number, options?: object)`
#### `{T} [options.fromUnit='']`
> unit of `num`.
#### `{T} [options.toUnit]`
> target unit, if empty, decent unit will be returned.
#### `{number|number[]} [options.radix=1024]`
> conversion radix.
If is an `array`, element is radix between every nearby units,
and filled with the last array element if length is more than 1 less than `options.units`.
#### `@param {T[]} [options.units=['', 'K', 'M', 'G', 'T', 'P', 'E']]`
> available units
See [decentUnit](index.js#L28)
## LICENSE
MIT