spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formula.CEILING
author
Ben Vogt <[email protected]>
date
2017-02-15 04:46:42
stats
2 file(s) changed, 46 insertions(+), 4 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 7f49d76..ab64252 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -75,7 +75,6 @@ var BIN2DEC = Formula["BIN2DEC"];
 6 var BIN2HEX = Formula["BIN2HEX"];
 7 var BIN2OCT = Formula["BIN2OCT"];
 8 var DECIMAL = Formula["DECIMAL"];
 9-var CEILING = Formula["CEILING"];
10 var COMBIN = Formula["COMBIN"];
11 var CONCATENATE = Formula["CONCATENATE"];
12 var CONVERT = Formula["CONVERT"];
13@@ -130,6 +129,31 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
14 var TRUNC = Formula["TRUNC"];
15 var YEARFRAC = Formula["YEARFRAC"];
16 
17+/**
18+ * Rounds a number up to the nearest integer multiple of specified significance.
19+ * @param values[0] The value to round up to the nearest integer multiple of factor.
20+ * @param values[1] The number to whose multiples value will be rounded.
21+ * @returns {number}
22+ * @constructor
23+ */
24+var CEILING = function (...values) : number {
25+  checkArgumentsAtWithin(values, 1, 2);
26+  var num = firstValueAsNumber(values[0]);
27+  if (values.length === 1) {
28+    return Math.ceil(num);
29+  }
30+  var significance = firstValueAsNumber(values[1]);
31+  if (significance === 0) {
32+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Function CEILING parameter 2 cannot be zero.");
33+  }
34+  var precision = -Math.floor(Math.log(significance) / Math.log(10));
35+  if (num >= 0) {
36+    return ROUND(Math.ceil(num / significance) * significance, precision);
37+  } else {
38+    return -ROUND(Math.floor(Math.abs(num) / significance) * significance, precision);
39+  }
40+};
41+
42 /**
43  * Rounds a number down to the nearest integer multiple of specified significance.
44  * @param values[0] The value to round down to the nearest integer multiple of factor.
45@@ -141,7 +165,7 @@ var FLOOR = function (...values) : number {
46   checkArgumentsAtWithin(values, 1, 2);
47   var num = firstValueAsNumber(values[0]);
48   if (values.length === 1) {
49-    return Math.round(num);
50+    return Math.floor(num);
51   }
52   var significance = firstValueAsNumber(values[1]);
53   if (significance === 0) {
54diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
55index 881332d..aec77ae 100644
56--- a/tests/FormulasTest.ts
57+++ b/tests/FormulasTest.ts
58@@ -268,7 +268,26 @@ assertEquals(BIN2OCT(1010101010), "7777777252");
59 
60 assertEquals(DECIMAL(199.99999), 199);
61 
62-assertEquals(CEILING(22.22, 0.1), 22.3);
63+// Test CEILING
64+assertEquals(CEILING(10.1), 11);
65+assertEquals(CEILING("10.1"), 11);
66+assertEquals(CEILING(10.11111111, 0.1), 10.2);
67+assertEquals(CEILING(10.22222222, 0.1), 10.3);
68+assertEquals(CEILING(10.33333333, 0.2), 10.4);
69+assertEquals(CEILING(10.33333333, 0.1), 10.4);
70+assertEquals(CEILING([10.33333333], 0.1), 10.4);
71+assertEquals(CEILING(10.22222222, 5), 15);
72+assertEquals(CEILING(10.22222222, 8), 16);
73+assertEquals(CEILING(10.22222222, true), 11);
74+catchAndAssertEquals(function() {
75+  CEILING(10, 0);
76+}, ERRORS.DIV_ZERO_ERROR);
77+catchAndAssertEquals(function() {
78+  CEILING(10, 1, 2);
79+}, ERRORS.NA_ERROR);
80+catchAndAssertEquals(function() {
81+  CEILING();
82+}, ERRORS.NA_ERROR);
83 
84 // Test CHAR
85 assertEquals(CHAR(97), "a");