Skip to content

Method converting numbers between different numeral systems.

Notifications You must be signed in to change notification settings

damianc/math-conv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

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

About

Method converting numbers between different numeral systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published