commit
message
Added Formulas.BIN2DEC
author
Ben Vogt <[email protected]>
date
2017-02-18 05:07:13
stats
3 file(s) changed,
39 insertions(+),
3 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
tests/SheetFormulaTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 0a8dec7..de62157 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -80,9 +80,9 @@ import {
6 } from "./Utils";
7 import { CellError } from "../Errors"
8 import * as ERRORS from "../Errors"
9+import {Cell} from "../Cell";
10
11 var ACCRINT = Formula["ACCRINT"];
12-var BIN2DEC = Formula["BIN2DEC"];
13 var BIN2HEX = Formula["BIN2HEX"];
14 var BIN2OCT = Formula["BIN2OCT"];
15 var DECIMAL = Formula["DECIMAL"];
16@@ -132,6 +132,29 @@ var SUMX2MY2 = Formula["SUMX2MY2"];
17 var SUMX2PY2 = Formula["SUMX2PY2"];
18 var YEARFRAC = Formula["YEARFRAC"];
19
20+/**
21+ * Converts a signed binary number to decimal format.
22+ * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to decimal, provided as a
23+ * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
24+ * in two's complement format.
25+ * @returns {number}
26+ * @constructor
27+ */
28+var BIN2DEC = function (...values) {
29+ ArgsChecker.checkLength(values, 1);
30+ var n = TypeCaster.firstValueAsString(values[0]);
31+ if (!(/^[01]{1,10}$/).test(n)) {
32+ throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2DEC ('"+n+"') is not a valid binary representation.");
33+ }
34+
35+ if (n.length === 10 && n.substring(0, 1) === '1') {
36+ return parseInt(n.substring(1), 2) - 512;
37+ } else {
38+ return parseInt(n, 2);
39+ }
40+};
41+
42+
43 /**
44 * Converts an angle value in degrees to radians.
45 * @param values[0] angle - The angle to convert from degrees to radians.
46diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
47index 84a0503..7305733 100644
48--- a/tests/FormulasTest.ts
49+++ b/tests/FormulasTest.ts
50@@ -285,7 +285,20 @@ catchAndAssertEquals(function() {
51 }, ERRORS.DIV_ZERO_ERROR);
52
53
54-assertEquals(BIN2DEC(1010101010), -342);
55+// Test BIN2DEC
56+assertEquals(BIN2DEC("1010101010"), -342);
57+assertEquals(BIN2DEC("10"), 2);
58+assertEquals(BIN2DEC(["10", "str"]), 2);
59+catchAndAssertEquals(function() {
60+ BIN2DEC("str");
61+}, ERRORS.NUM_ERROR);
62+catchAndAssertEquals(function() {
63+ BIN2DEC();
64+}, ERRORS.NA_ERROR);
65+catchAndAssertEquals(function() {
66+ BIN2DEC("10", "10");
67+}, ERRORS.NA_ERROR);
68+
69
70 assertEquals(BIN2HEX(1010101010), "fffffffeaa");
71
72diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
73index f2fbf4e..35d4d5c 100644
74--- a/tests/SheetFormulaTest.ts
75+++ b/tests/SheetFormulaTest.ts
76@@ -81,7 +81,7 @@ testFormula("=AVERAGEA(10, 20, 4.1)", 11.366666666666667);
77 testFormula("=AVERAGEIF([1, 5, 10], '>2')", 7.5);
78
79 // Test BIN2DEC
80-testFormula("=BIN2DEC(1010101010)", -342);
81+testFormula("=BIN2DEC('1010101010')", -342);
82
83 // Test BINOMINV
84 // TODO: This should work.