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.FLOOR
author
Ben Vogt <[email protected]>
date
2017-02-15 04:41:31
stats
2 file(s) changed, 48 insertions(+), 0 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index f200c45..7f49d76 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -130,6 +130,31 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
 6 var TRUNC = Formula["TRUNC"];
 7 var YEARFRAC = Formula["YEARFRAC"];
 8 
 9+/**
10+ * Rounds a number down to the nearest integer multiple of specified significance.
11+ * @param values[0] The value to round down to the nearest integer multiple of factor.
12+ * @param values[1] The number to whose multiples value will be rounded.
13+ * @returns {number}
14+ * @constructor
15+ */
16+var FLOOR = function (...values) : number {
17+  checkArgumentsAtWithin(values, 1, 2);
18+  var num = firstValueAsNumber(values[0]);
19+  if (values.length === 1) {
20+    return Math.round(num);
21+  }
22+  var significance = firstValueAsNumber(values[1]);
23+  if (significance === 0) {
24+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Function FLOOR parameter 2 cannot be zero.");
25+  }
26+  significance = significance ? Math.abs(significance) : 1;
27+  var precision = -Math.floor(Math.log(significance) / Math.log(10));
28+  if (num >= 0) {
29+    return ROUND(Math.floor(num / significance) * significance, precision);
30+  }
31+  return -ROUND(Math.floor(Math.abs(num) / significance) * significance, precision);
32+};
33+
34 /**
35  * Returns one value if a logical expression is TRUE and another if it is FALSE.
36  * @param values[0] An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE.
37@@ -461,6 +486,7 @@ export {
38   FALSE,
39   FISHER,
40   FISHERINV,
41+  FLOOR,
42   IF,
43   INT,
44   ISEVEN,
45diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
46index d4593cf..881332d 100644
47--- a/tests/FormulasTest.ts
48+++ b/tests/FormulasTest.ts
49@@ -4,7 +4,7 @@ import { ABS, ACCRINT, ACOS, ACOSH, ACOTH, AND, ARABIC, ASIN, ASINH, ATAN, ATAN2
50     CORREL, COS, PI, COSH, COT, COTH, COUNT, COUNTA, COUNTIF, COUNTIFS, COUNTUNIQUE,
51     COVARIANCEP, COVARIANCES, CSC, CSCH, CUMIPMT, CUMPRINC, DATE, DATEVALUE, DAY, DAYS, DAYS360,
52     DB, DDB, DEC2BIN, DEC2HEX, DEC2OCT, DEGREES, DELTA, DEVSQ, DOLLAR, DOLLARDE, DOLLARFR, EDATE,
53-    EFFECT, EOMONTH, ERF, ERFC, EVEN, EXACT, EXPONDIST, FALSE, __COMPLEX, FISHER, FISHERINV, IF,
54+    EFFECT, EOMONTH, ERF, ERFC, EVEN, EXACT, EXPONDIST, FALSE, FLOOR, __COMPLEX, FISHER, FISHERINV, IF,
55     INT, ISEVEN, ISODD, LN, LOG, LOG10, MAX, MAXA, MEDIAN, MIN, MINA, MOD, NOT, TRUE, ODD, OR,
56     POWER, ROUND, ROUNDDOWN, ROUNDUP, SIN, SINH, SPLIT, SQRT, SQRTPI, SUM, SUMIF, SUMPRODUCT,
57     SUMSQ, SUMX2MY2, SUMX2PY2, TAN, TANH, TRUNC, XOR, YEARFRAC } from "../src/RawFormulas/RawFormulas"
58@@ -658,6 +658,27 @@ assertEquals(FISHER(0.962), 1.972066740199461);
59 
60 assertEquals(FISHERINV(0.962), 0.7451676440945232);
61 
62+// Test FLOOR
63+assertEquals(FLOOR(10.1), 10);
64+assertEquals(FLOOR("10.1"), 10);
65+assertEquals(FLOOR(10.11111111, 0.1), 10.1);
66+assertEquals(FLOOR(10.22222222, 0.1), 10.2);
67+assertEquals(FLOOR(10.33333333, 0.2), 10.2);
68+assertEquals(FLOOR(10.33333333, 0.1), 10.3);
69+assertEquals(FLOOR([10.33333333], 0.1), 10.3);
70+assertEquals(FLOOR(10.22222222, 5), 10);
71+assertEquals(FLOOR(10.22222222, 8), 8);
72+assertEquals(FLOOR(10.22222222, true), 10);
73+catchAndAssertEquals(function() {
74+  FLOOR(10, 0);
75+}, ERRORS.DIV_ZERO_ERROR);
76+catchAndAssertEquals(function() {
77+  FLOOR(10, 1, 2);
78+}, ERRORS.NA_ERROR);
79+catchAndAssertEquals(function() {
80+  FLOOR();
81+}, ERRORS.NA_ERROR);
82+
83 
84 // Test IF
85 assertEquals(IF(true, "hit", "miss"), "hit");