math-conv

2.1.2 • Public • Published

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() and octToHex()
  • value-giving: binToOct(), decToOct() and hexToOct()
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 base 2
  • Math.DEC for base 10
  • Math.HEX for base 16

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

Readme

Keywords

none

Package Sidebar

Install

npm i math-conv

Weekly Downloads

0

Version

2.1.2

License

ISC

Unpacked Size

3.97 kB

Total Files

3

Last publish

Collaborators

  • damianc