decimal-to-binary-converter
This library is an algorithm for converting decimal to binary and show each steps of calculation.
This library was generated by essay
(https://github.com/dtinth/essay).
Calculation
This library will show how to calculate them.
// 12.875 to binary
Converting Integer 12 to binary
12 % 2 = 0
12 / 2 = 6
6 % 2 = 0
6 / 2 = 3
3 % 2 = 1
3 / 2 = 1
1 % 2 = 1
1 / 2 = 0
[Answer = 1100]
Converting Float 0.875 to binary
0.875 * 2 = 1.75
1
0.75 * 2 = 1.5
1
0.5 * 2 = 1
1
[Answer = 0.111]
======== binary of 12.875 is 1100.111 ========
Getting started
- Install this library
npm i --save decimal-to-binary-converter
- import function from this library
API
converter
is a function that can be used.
Limitation: floating point of binary maximum default is 10
// main.js
Example
There are some examples about simple results of converter
.
// converter.test.js
Implementation
converter
contains two parts
convertIntegerToBinary
convertFloatToBinary
// converter.js const convertIntegerToBinary = { const result = console while number !== 0 console console result number = number / 2 | 0 const answer = _ console return answer} const convertFloatToBinary = { const result = console while !_ && number % 1 !== console number *= 2 console result number %= 1 if resultlength > 10 break const answer = `0.` console return answer} const converter = { const intPart = number | 0 const floatPart = number % 1 const resultIntPart = const resultFloatPart = const result = `.` console return result}