commit
message
Added Formulas.DEC2BIN
author
Ben Vogt <[email protected]>
date
2017-02-16 05:00:35
stats
2 file(s) changed,
102 insertions(+),
2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 676ab5b..429c676 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -105,7 +105,6 @@ var DAYS = Formula["DAYS"];
6 var DAYS360 = Formula["DAYS360"];
7 var DB = Formula["DB"];
8 var DDB = Formula["DDB"];
9-var DEC2BIN = Formula["DEC2BIN"];
10 var DEC2HEX = Formula["DEC2HEX"];
11 var DEC2OCT = Formula["DEC2OCT"];
12 var DEGREES = Formula["DEGREES"];
13@@ -136,6 +135,78 @@ var SUMX2MY2 = Formula["SUMX2MY2"];
14 var SUMX2PY2 = Formula["SUMX2PY2"];
15 var YEARFRAC = Formula["YEARFRAC"];
16
17+/**
18+ * Converts a decimal number to signed binary format.
19+ * @param values[0] decimal_number - The decimal value to be converted to signed binary, provided as a string. For this
20+ * function, this value has a maximum of 511 if positive, and a minimum of -512 if negative.
21+ * @param values[1] significant_digits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
22+ * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
23+ * number of digits reaches significant_digits.
24+ * @returns {string} signed binary string representation of the input decimal number.
25+ * @constructor
26+ */
27+var DEC2BIN = function (...values) {
28+ ArgsChecker.checkLengthWithin(values, 1, 2);
29+ var n = TypeCaster.firstValueAsNumber(values[0]);
30+ if (n < 0) {
31+ n = Math.ceil(n);
32+ }
33+ if (n > 0) {
34+ n = Math.floor(n);
35+ }
36+ if (n === 0 || n === 1) {
37+ return n.toString();
38+ }
39+ var p = 10;
40+ var placesPresent = false;
41+ if (values.length === 2) {
42+ p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
43+ placesPresent = true;
44+ }
45+
46+ if (n < -512 || n > 511) {
47+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 1 value is " + n + ". Valid values are between -512 and 511 inclusive.");
48+ }
49+ if (p < 1 || p > 10) {
50+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
51+ }
52+
53+ // Ignore places and return a 10-character binary number if number is negative
54+ if (n < 0) {
55+ var count = (9 - (512 + n).toString(2).length);
56+ var st = "";
57+ for (var i = 0; i < count; i++) {
58+ st += "0";
59+ }
60+ return "1" + st + (512 + n).toString(2);
61+ }
62+
63+ // Convert decimal number to binary
64+ var result = parseInt(n.toString(), 10).toString(2);
65+
66+ // Pad return value with leading 0s (zeros) if necessary
67+ if (p >= result.length) {
68+ var str = "";
69+ for (var i = 0; i < (p - result.length); i++) {
70+ str += "0";
71+ }
72+ var workingString = str + result;
73+ if (!placesPresent) {
74+ var returnString = "";
75+ for (var i = 0; i < workingString.length; i++) {
76+ var char = workingString[i];
77+ if (char === "1") {
78+ break;
79+ }
80+ returnString = workingString.slice(i+1);
81+ }
82+ return returnString;
83+ }
84+ return workingString;
85+ }
86+};
87+
88+
89
90 export {
91 __COMPLEX,
92diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
93index 7711933..f0d9cac 100644
94--- a/tests/FormulasTest.ts
95+++ b/tests/FormulasTest.ts
96@@ -606,7 +606,37 @@ assertEquals(DB(100, 50, 10, 2, 12), 6.2511);
97
98 assertEquals(DDB(100, 50, 10, 2, 2.25), 17.4375);
99
100-assertEquals(DEC2BIN("100", 8), "01100100");
101+// Test DEC2BIN
102+assertEquals(DEC2BIN(100), "1100100");
103+assertEquals(DEC2BIN(22), "10110");
104+assertEquals(DEC2BIN(22.11), "10110");
105+assertEquals(DEC2BIN(22.77), "10110");
106+assertEquals(DEC2BIN("22.77"), "10110");
107+assertEquals(DEC2BIN(100, 8), "01100100");
108+assertEquals(DEC2BIN(100, 7), "1100100");
109+assertEquals(DEC2BIN(100, 10), "0001100100");
110+assertEquals(DEC2BIN(-100), "1110011100");
111+assertEquals(DEC2BIN("-22.77"), "1111101010");
112+assertEquals(DEC2BIN(-22.11), "1111101010");
113+assertEquals(DEC2BIN(-22), "1111101010");
114+assertEquals(DEC2BIN(false), "0");
115+assertEquals(DEC2BIN(true), "1");
116+catchAndAssertEquals(function() {
117+ DEC2BIN(100, 0);
118+}, ERRORS.NUM_ERROR);
119+catchAndAssertEquals(function() {
120+ DEC2BIN(513, 10);
121+}, ERRORS.NUM_ERROR);
122+catchAndAssertEquals(function() {
123+ DEC2BIN(100, 100, 10);
124+}, ERRORS.NA_ERROR);
125+catchAndAssertEquals(function() {
126+ DEC2BIN();
127+}, ERRORS.NA_ERROR);
128+catchAndAssertEquals(function() {
129+ DEC2BIN("str");
130+}, ERRORS.VALUE_ERROR);
131+
132
133 assertEquals(DEC2HEX("100"), "64");
134