spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.DEC2HEX
author
Ben Vogt <[email protected]>
date
2017-02-17 22:58:24
stats
2 file(s) changed, 92 insertions(+), 3 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index 429c676..e2719ff 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 DEC2HEX = Formula["DEC2HEX"];
 10 var DEC2OCT = Formula["DEC2OCT"];
 11 var DEGREES = Formula["DEGREES"];
 12 var DEVSQ = Formula["DEVSQ"];
 13@@ -135,6 +134,59 @@ var SUMX2MY2 = Formula["SUMX2MY2"];
 14 var SUMX2PY2 = Formula["SUMX2PY2"];
 15 var YEARFRAC = Formula["YEARFRAC"];
 16 
 17+/**
 18+ * Converts a decimal number to signed hexadecimal format.
 19+ * @param values[0] decimal_number - The decimal value to be converted to signed hexadecimal, provided as a string. This
 20+ * value has a maximum of 549755813887 if positive, and a minimum of -549755814888 if negative.
 21+ * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
 22+ * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
 23+ * total number of digits reaches significant_digits. This value is ignored if decimal_number is negative.
 24+ * @returns {string} hexadecimal string representation of the decimal number
 25+ * @constructor
 26+ */
 27+var DEC2HEX = function (...values) : string {
 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+  var p = 10;
 37+  var placesPresent = false;
 38+  if (values.length === 2) {
 39+    p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
 40+    placesPresent = true;
 41+  }
 42+  if (n < -549755813888 || n > 549755813887) {
 43+    throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 1 value is " + n + ". Valid values are between -549755813888 and 549755813887 inclusive.");
 44+  }
 45+  if (p < 1 || p > 10) {
 46+    throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
 47+  }
 48+  // Ignore places and return a 10-character hexadecimal number if number is negative
 49+  if (n < 0) {
 50+    return (1099511627776 + n).toString(16).toUpperCase();
 51+  }
 52+
 53+  // Convert decimal number to hexadecimal
 54+  var result = parseInt(n.toString(), 10).toString(16).toUpperCase();
 55+  if (!placesPresent) {
 56+    return result;
 57+  } else {
 58+    // Pad return value with leading 0s (zeros) if necessary (using Underscore.string)
 59+    if (p >= result.length) {
 60+      // Throw NUM error?
 61+    }
 62+    var str = "";
 63+    for (var i = 0; i < p - result.length; i++) {
 64+      str += "0";
 65+    }
 66+    return str + result.toUpperCase();
 67+  }
 68+};
 69+
 70 /**
 71  * Converts a decimal number to signed binary format.
 72  * @param values[0] decimal_number - The decimal value to be converted to signed binary, provided as a string. For this
 73@@ -145,7 +197,7 @@ var YEARFRAC = Formula["YEARFRAC"];
 74  * @returns {string} signed binary string representation of the input decimal number.
 75  * @constructor
 76  */
 77-var DEC2BIN = function (...values) {
 78+var DEC2BIN = function (...values) : string {
 79   ArgsChecker.checkLengthWithin(values, 1, 2);
 80   var n = TypeCaster.firstValueAsNumber(values[0]);
 81   if (n < 0) {
 82diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 83index f0d9cac..d10400e 100644
 84--- a/tests/FormulasTest.ts
 85+++ b/tests/FormulasTest.ts
 86@@ -607,12 +607,14 @@ assertEquals(DB(100, 50, 10, 2, 12), 6.2511);
 87 assertEquals(DDB(100, 50, 10, 2, 2.25), 17.4375);
 88 
 89 // Test DEC2BIN
 90+assertEquals(DEC2BIN([100]), "1100100");
 91 assertEquals(DEC2BIN(100), "1100100");
 92 assertEquals(DEC2BIN(22), "10110");
 93 assertEquals(DEC2BIN(22.11), "10110");
 94 assertEquals(DEC2BIN(22.77), "10110");
 95 assertEquals(DEC2BIN("22.77"), "10110");
 96 assertEquals(DEC2BIN(100, 8), "01100100");
 97+assertEquals(DEC2BIN([100], [8]), "01100100");
 98 assertEquals(DEC2BIN(100, 7), "1100100");
 99 assertEquals(DEC2BIN(100, 10), "0001100100");
100 assertEquals(DEC2BIN(-100), "1110011100");
101@@ -637,8 +639,42 @@ catchAndAssertEquals(function() {
102   DEC2BIN("str");
103 }, ERRORS.VALUE_ERROR);
104 
105+// Test DEC2HEX
106+assertEquals(DEC2HEX([100]), "64");
107+assertEquals(DEC2HEX(100), "64");
108+assertEquals(DEC2HEX(22), "16");
109+assertEquals(DEC2HEX(22.11), "16");
110+assertEquals(DEC2HEX(22.77), "16");
111+assertEquals(DEC2HEX("22.77"), "16");
112+assertEquals(DEC2HEX(100, 8), "00000064");
113+assertEquals(DEC2HEX([100], [8]), "00000064");
114+assertEquals(DEC2HEX(100, 7), "0000064");
115+assertEquals(DEC2HEX(100, 10), "0000000064");
116+assertEquals(DEC2HEX(-100), "FFFFFFFF9C");
117+assertEquals(DEC2HEX("-22.77"), "FFFFFFFFEA");
118+assertEquals(DEC2HEX(-22.11), "FFFFFFFFEA");
119+assertEquals(DEC2HEX(-22), "FFFFFFFFEA");
120+assertEquals(DEC2HEX(false), "0");
121+assertEquals(DEC2HEX(true), "1");
122+catchAndAssertEquals(function() {
123+  DEC2HEX(100, 0);
124+}, ERRORS.NUM_ERROR);
125+catchAndAssertEquals(function() {
126+  DEC2HEX(549755813889, 10);
127+}, ERRORS.NUM_ERROR);
128+catchAndAssertEquals(function() {
129+  DEC2HEX(54975581, -10);
130+}, ERRORS.NUM_ERROR);
131+catchAndAssertEquals(function() {
132+  DEC2HEX(100, 100, 10);
133+}, ERRORS.NA_ERROR);
134+catchAndAssertEquals(function() {
135+  DEC2HEX();
136+}, ERRORS.NA_ERROR);
137+catchAndAssertEquals(function() {
138+  DEC2HEX("str");
139+}, ERRORS.VALUE_ERROR);
140 
141-assertEquals(DEC2HEX("100"), "64");
142 
143 assertEquals(DEC2OCT("100"), "144");
144