https://github.com/javiercejudo/positional-notation
Calculate the decimal equivalent given a base, a position and a decimal value
https://github.com/javiercejudo/positional-notation
Last synced: 5 months ago
JSON representation
Calculate the decimal equivalent given a base, a position and a decimal value
- Host: GitHub
- URL: https://github.com/javiercejudo/positional-notation
- Owner: javiercejudo
- License: mit
- Created: 2016-02-13T00:15:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-15T13:14:07.000Z (almost 10 years ago)
- Last Synced: 2025-07-26T10:15:34.853Z (6 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/positional-notation
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# positional-notation
[](https://travis-ci.org/javiercejudo/positional-notation)
[](https://coveralls.io/r/javiercejudo/positional-notation?branch=master)
[](https://codeclimate.com/github/javiercejudo/positional-notation)
Calculate the decimal equivalent given a base, a position and a decimal value
## Install
npm i positional-notation
## Basic usage
```js
var fn = require('positional-notation');
fn(60, [1, 32]); //=> 1920
fn(5, [4, 3]); //=> 1875
```
## Arbitrary precision
```js
var Decimal = require('arbitrary-precision')(require('decimaljs-adapter'));
Decimal.setPrecision(50);
var d = require('to-decimal-arbitrary-precision')(Decimal);
fn.raw(d, 10, [2, 3/10]); // d(30)
```
where `d` is a [`toDecimal`](https://github.com/javiercejudo/to-decimal) with at least `times` and `pow`.
## Use case: functional hex to dec
```js
const R = require('ramda');
const fn = require('positional-notation');
const symbols = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
};
const posNotation = fn(Object.keys(symbols).length);
const hexToDec = R.pipe(
R.toUpper,
R.split(''),
R.reverse,
R.map(R.prop(R.__, symbols)),
R.addIndex(R.map)(fn.mapper(posNotation)),
R.sum
);
hexToDec('7E0'); //=> 2016
```
See [spec](test/spec.js).