positional-notation
v2.0.0
Published
Calculate the decimal equivalent given a base, a position and a decimal value
Downloads
42
Maintainers
Readme
positional-notation
Calculate the decimal equivalent given a base, a position and a decimal value
Install
npm i positional-notation
Basic usage
var fn = require('positional-notation');
fn(60, [1, 32]); //=> 1920
fn(5, [4, 3]); //=> 1875
Arbitrary precision
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
with at least times
and pow
.
Use case: functional hex to dec
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.