Adds new method - Math.conv()
, converting numbers between different numeral systems.
Math.conv(sourceSystem, targetSystem, value)
npm i -P math-conv
Math.conv(Math.DEC, Math.BIN, 4)
// '100'
Math.conv(Math.BIN, Math.DEC, 100)
// '4'
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'
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'
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