-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.js
92 lines (80 loc) · 2.17 KB
/
draft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
validateBase,
reverseString,
convertNumToAlphabet,
convertAlphabetToNum,
} from "./js/utility.js";
function decimalToAny(input, base) {
if (!validateBase(base)) return;
input = Number(input);
let output = "";
let pointValue = "";
let quotient, pointQuotient;
let isPoint = false;
if (input % 1 !== 0) {
isPoint = true;
}
if (isPoint) {
quotient = Number(input.toString().split(".")[0]);
pointQuotient = Number("0." + input.toString().split(".")[1]);
let count = 0;
while (count < 5) {
let value = pointQuotient * base;
let remainder = Math.floor(value);
if (remainder >= 10) {
pointValue += convertNumToAlphabet(remainder);
} else {
pointValue += remainder.toString();
}
pointQuotient = Number("0." + value.toString().split(".")[1]);
count++;
}
} else {
quotient = input;
}
while (quotient !== 0) {
let remainder = quotient % base;
quotient = Math.floor(quotient / base);
if (remainder >= 10) {
output += convertNumToAlphabet(remainder);
} else {
output += remainder.toString();
}
}
return reverseString(output) + "." + pointValue;
}
console.log(decimalToAny(3315.3, 16));
function anyToDecimal(input, base) {
if (!validateBase(base)) return;
input = input.toString();
let isHexaDecimal = false;
let isPoint = false;
if (!Number(input)) {
isHexaDecimal = true;
}
if (input.split(".").length == 2) {
isPoint = true;
}
let output = 0;
let pointValue = 0;
let quotient, pointQuotient;
if (isPoint) {
let twoPoint = input.split(".");
quotient = twoPoint[0];
pointQuotient = twoPoint[1];
pointQuotient.split("").forEach((item, index) => {
if (isHexaDecimal && !Number(item)) item = convertAlphabetToNum(item);
pointValue += Number(item) * Math.pow(base, -(index + 1));
});
} else {
quotient = input;
}
reverseString(quotient)
.split("")
.forEach((item, index) => {
if (isHexaDecimal && !Number(item)) item = convertAlphabetToNum(item);
output += Number(item) * Math.pow(base, index);
});
return output + pointValue;
}
console.log(anyToDecimal("55.5", 8));