math-conv
v2.1.2
Published
Adds new method - `Math.conv()`, converting numbers between different numeral systems.
Downloads
2
Readme
math-conv
Adds new method - Math.conv()
, converting numbers between different numeral systems.
Math.conv(sourceSystem, targetSystem, value)
Installation
npm i -P math-conv
Examples
Math.conv(Math.DEC, Math.BIN, 4)
// '100'
Math.conv(Math.BIN, Math.DEC, 100)
// '4'
Specialized Methods
The Math.conv
contains a getter that provides an interface to utilize specialized methods:
binToDec()
decToBin()
hexToDec()
decToHex()
binToHex()
hexToBin()
Math.conv.decToBin(4)
// '100'
Math.conv.binToDec(100)
// '4'
Custom Systems
To add a new system use Math.conv.addSystem()
method:
Math.conv.addSystem(code, base, addMathConstant = false)
First line in the code below will cause new methods will be accessible:
- value-taking:
octToBin()
,octToDec()
andoctToHex()
- value-giving:
binToOct()
,decToOct()
andhexToOct()
Math.conv.addSystem('oct', 8);
const DEC = 24;
Math.conv.decToHex(DEC)
// '18'
Math.conv.hexToOct(18)
// '30'
Math.conv.decToOct(DEC)
// '30'
Built-ins
Math.BIN
for base2
Math.DEC
for base10
Math.HEX
for base16
When adding a new system with the addSystem()
method, pass third parameter true
to extend Math
with a specific constant (uppercase):
Math.conv.addSystem('oct', 8, true);
Math.OCT
// 8