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.DEC2OCT
author
Ben Vogt <[email protected]>
date
2017-02-18 01:03:29
stats
2 file(s) changed, 88 insertions(+), 3 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index e2719ff..614cd89 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 DEC2OCT = Formula["DEC2OCT"];
 10 var DEGREES = Formula["DEGREES"];
 11 var DEVSQ = Formula["DEVSQ"];
 12 var DOLLAR = Formula["DOLLAR"];
 13@@ -134,6 +133,58 @@ var SUMX2MY2 = Formula["SUMX2MY2"];
 14 var SUMX2PY2 = Formula["SUMX2PY2"];
 15 var YEARFRAC = Formula["YEARFRAC"];
 16 
 17+/**
 18+ * Converts a decimal number to signed octal format.
 19+ * @param values[0] decimal_number - The decimal value to be converted to signed octal,provided as a string. For this
 20+ * function, this value has a maximum of 536870911 if positive, and a minimum of -53687092 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} octal string representation of the decimal number
 25+ * @constructor
 26+ */
 27+var DEC2OCT = 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 < -53687092 || n > 536870911) {
 43+    throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 1 value is " + n + ". Valid values are between -53687092 and 536870911 inclusive.");
 44+  }
 45+  if (p < 1 || p > 10) {
 46+    throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
 47+  }
 48+  if (n < 0) {
 49+    return (1073741824 + n).toString(8).toUpperCase();
 50+  }
 51+
 52+  // Convert decimal number to hexadecimal
 53+  var result = parseInt(n.toString(), 10).toString(8).toUpperCase();
 54+  if (!placesPresent) {
 55+    return result;
 56+  } else {
 57+    if (p >= result.length) {
 58+      // Throw NUM error?
 59+    }
 60+    var str = "";
 61+    for (var i = 0; i < p - result.length; i++) {
 62+      str += "0";
 63+    }
 64+    return str + result.toUpperCase();
 65+  }
 66+};
 67+
 68+
 69 /**
 70  * Converts a decimal number to signed hexadecimal format.
 71  * @param values[0] decimal_number - The decimal value to be converted to signed hexadecimal, provided as a string. This
 72@@ -175,7 +226,6 @@ var DEC2HEX = function (...values) : string {
 73   if (!placesPresent) {
 74     return result;
 75   } else {
 76-    // Pad return value with leading 0s (zeros) if necessary (using Underscore.string)
 77     if (p >= result.length) {
 78       // Throw NUM error?
 79     }
 80diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 81index d10400e..dc20db3 100644
 82--- a/tests/FormulasTest.ts
 83+++ b/tests/FormulasTest.ts
 84@@ -676,7 +676,42 @@ catchAndAssertEquals(function() {
 85 }, ERRORS.VALUE_ERROR);
 86 
 87 
 88-assertEquals(DEC2OCT("100"), "144");
 89+// Test DEC2OCT
 90+assertEquals(DEC2OCT([100]), "144");
 91+assertEquals(DEC2OCT(100), "144");
 92+assertEquals(DEC2OCT(22), "26");
 93+assertEquals(DEC2OCT(22.11), "26");
 94+assertEquals(DEC2OCT(22.77), "26");
 95+assertEquals(DEC2OCT("22.77"), "26");
 96+assertEquals(DEC2OCT(100, 8), "00000144");
 97+assertEquals(DEC2OCT([100], [8]), "00000144");
 98+assertEquals(DEC2OCT(100, 7), "0000144");
 99+assertEquals(DEC2OCT(100, 10), "0000000144");
100+assertEquals(DEC2OCT(-100), "7777777634");
101+assertEquals(DEC2OCT("-22.77"), "7777777752");
102+assertEquals(DEC2OCT(-22.11), "7777777752");
103+assertEquals(DEC2OCT(-22), "7777777752");
104+assertEquals(DEC2OCT(false), "0");
105+assertEquals(DEC2OCT(true), "1");
106+catchAndAssertEquals(function() {
107+  DEC2OCT(100, 0);
108+}, ERRORS.NUM_ERROR);
109+catchAndAssertEquals(function() {
110+  DEC2OCT(536870913, 10);
111+}, ERRORS.NUM_ERROR);
112+catchAndAssertEquals(function() {
113+  DEC2OCT(536870910, -10);
114+}, ERRORS.NUM_ERROR);
115+catchAndAssertEquals(function() {
116+  DEC2OCT(100, 100, 10);
117+}, ERRORS.NA_ERROR);
118+catchAndAssertEquals(function() {
119+  DEC2OCT();
120+}, ERRORS.NA_ERROR);
121+catchAndAssertEquals(function() {
122+  DEC2OCT("str");
123+}, ERRORS.VALUE_ERROR);
124+
125 
126 assertEquals(DEGREES(PI()), 180);
127