spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Adding Formula.ROUNDDOWN
author
Ben Vogt <[email protected]>
date
2017-02-09 05:25:20
stats
2 file(s) changed, 35 insertions(+), 1 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 63940ea..12dd20e 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -221,7 +221,22 @@ var ROUND = function (...values) {
 6   return Math.round(n * Math.pow(10, d)) / Math.pow(10, d);
 7 };
 8 
 9-var ROUNDDOWN = Formula["ROUNDDOWN"];
10+/**
11+ * Rounds a number to a certain number of decimal places, always rounding down to the next valid increment.
12+ * @param values[0] The value to round to places number of places, always rounding down.
13+ * @param values[1] (optional) The number of decimal places to which to round.
14+ * @returns {number}
15+ * @constructor
16+ */
17+var ROUNDDOWN = function (...values) {
18+  checkArgumentsAtWithin(values, 1, 2);
19+  var n = firstValueAsNumber(values[0]);
20+  if (values.length === 1) {
21+    return Math.floor(n);
22+  }
23+  var d = firstValueAsNumber(values[1]);
24+  return Math.floor(n * Math.pow(10, d)) / Math.pow(10, d);
25+};
26 var ROUNDUP = Formula["ROUNDUP"];
27 var SPLIT = Formula["SPLIT"];
28 var SQRTPI = Formula["SQRTPI"];
29diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
30index 12806f6..bd01673 100644
31--- a/tests/FormulasTest.ts
32+++ b/tests/FormulasTest.ts
33@@ -978,9 +978,28 @@ catchAndAssertEquals(function() {
34 catchAndAssertEquals(function() {
35   ROUND(99.44, 1, 44);
36 }, ERRORS.NA_ERROR);
37+catchAndAssertEquals(function() {
38+  ROUND(99.999, "str");
39+}, ERRORS.VALUE_ERROR);
40 
41 
42+// Test ROUNDDOWN
43 assertEquals(ROUNDDOWN(99.46, 1), 99.4);
44+assertEquals(ROUNDDOWN(99.99, 1), 99.9);
45+assertEquals(ROUNDDOWN(99.5555555555555, 9), 99.555555555);
46+assertEquals(ROUNDDOWN(99.99), 99);
47+assertEquals(ROUNDDOWN("99.99"), 99);
48+assertEquals(ROUNDDOWN([99.46666, 22.222], [1, 4]), 99.4);
49+catchAndAssertEquals(function() {
50+  ROUNDDOWN();
51+}, ERRORS.NA_ERROR);
52+catchAndAssertEquals(function() {
53+  ROUNDDOWN(99.44, 1, 44);
54+}, ERRORS.NA_ERROR);
55+catchAndAssertEquals(function() {
56+  ROUNDDOWN(99.999, "str");
57+}, ERRORS.VALUE_ERROR);
58+
59 
60 assertEquals(ROUNDUP(99.46, 1), 99.5);
61