commit
message
Formulas.EVEN written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 17:14:47
stats
2 file(s) changed,
37 insertions(+),
1 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
2index 4f7aac3..d84366f 100644
3--- a/src/RawFormulas.ts
4+++ b/src/RawFormulas.ts
5@@ -433,7 +433,26 @@ var EOMONTH = function (start_date, months) {
6 };
7 var ERF = Formula["ERF"];
8 var ERFC = Formula["ERFC"];
9-var EVEN = Formula["EVEN"];
10+
11+
12+/**
13+ * Rounds a number up to the nearest even integer.
14+ * @param values[0] The value to round to the next greatest even number.
15+ * @returns {number} next greatest even number
16+ * @constructor
17+ */
18+var EVEN = function (...values) : number {
19+ checkArgumentsLength(values, 1);
20+ if (values[0] instanceof Array) {
21+ if (values[0].length === 0) {
22+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
23+ }
24+ return EVEN(values[0][0]);
25+ }
26+ var X = valueToNumber(values[0]);
27+ return X % 2 === 1 ? X + 1 : X;
28+};
29+
30 var EXACT = Formula["EXACT"];
31 var EXPONDIST = Formula["EXPONDIST"];
32 var FALSE = Formula["FALSE"];
33diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
34index 69b7624..42deefe 100644
35--- a/tests/FormulasTest.ts
36+++ b/tests/FormulasTest.ts
37@@ -348,7 +348,24 @@ assertEquals(ERF(2), 0.9953222650189527);
38
39 assertEquals(ERFC(2), 0.004677734981047288);
40
41+// Test EVEN
42 assertEquals(EVEN(3), 4);
43+assertEquals(EVEN(4), 4);
44+assertEquals(EVEN(5), 6);
45+assertEquals(EVEN("4"), 4);
46+assertEquals(EVEN(false), 0);
47+assertEquals(EVEN(true), 2);
48+assertEquals(EVEN([11, 22]), 12);
49+assertEquals(EVEN([10, 22, "str"]), 10);
50+catchAndAssertEquals(function() {
51+ EVEN();
52+}, ERRORS.NA_ERROR);
53+catchAndAssertEquals(function() {
54+ EVEN(1, 2, 3);
55+}, ERRORS.NA_ERROR);
56+catchAndAssertEquals(function() {
57+ EVEN("str");
58+}, ERRORS.VALUE_ERROR);
59
60 assertEquals(EXACT("m", "M"), false);
61