spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[MROUND] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-16 00:51:40
stats
8 file(s) changed, 82 insertions(+), 8 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 59b3924..1e04354 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1215,6 +1215,16 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### MROUND 
 11+
 12+```
 13+  Rounds a number to the nearest integer multiple of another. 
 14+@param value - value to round. 
 15+@param factor - multiple. 
 16+@returns {number} 
 17+@constructor
 18+```
 19 ## Statistical
 20 
 21 
 22diff --git a/TODO.md b/TODO.md
 23index 035cf01..e8ea9a8 100644
 24--- a/TODO.md
 25+++ b/TODO.md
 26@@ -29,10 +29,10 @@ For example 64 tbs to a qt.
 27 * ISBLANK - May require changes to parser.js
 28 * ISEMAIL
 29 * ISERR
 30-* ISERROR
 31-* ISFORMULA
 32-* ISNA
 33-* ISREF
 34+* ISERROR - Requires changes to parser.js
 35+* ISFORMULA - Requires changes to parser.js
 36+* ISNA - Requires changes to parser.js
 37+* ISREF - Requires changes to parser.js
 38 * ISURL
 39 * N
 40 * TYPE
 41@@ -52,7 +52,6 @@ For example 64 tbs to a qt.
 42 * VLOOKUP
 43 * COUNTBLANK
 44 * FACTDOUBLE
 45-* MROUND
 46 * MULTINOMIAL
 47 * SERIESSUM
 48 * SUBTOTAL
 49diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 50index 4b97f3e..f3cf143 100644
 51--- a/dist/Formulas/AllFormulas.js
 52+++ b/dist/Formulas/AllFormulas.js
 53@@ -71,6 +71,7 @@ exports.PRODUCT = Math_1.PRODUCT;
 54 exports.QUOTIENT = Math_1.QUOTIENT;
 55 exports.UPLUS = Math_1.UPLUS;
 56 exports.UMINUS = Math_1.UMINUS;
 57+exports.MROUND = Math_1.MROUND;
 58 var Info_1 = require("./Info");
 59 exports.NA = Info_1.NA;
 60 exports.ISTEXT = Info_1.ISTEXT;
 61diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 62index 33a2177..5788e20 100644
 63--- a/dist/Formulas/Math.js
 64+++ b/dist/Formulas/Math.js
 65@@ -1282,3 +1282,23 @@ var UMINUS = function (value) {
 66     return n * -1;
 67 };
 68 exports.UMINUS = UMINUS;
 69+/**
 70+ * Rounds a number to the nearest integer multiple of another.
 71+ * @param value - value to round.
 72+ * @param factor - multiple.
 73+ * @returns {number}
 74+ * @constructor
 75+ */
 76+var MROUND = function (value, factor) {
 77+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "MROUND");
 78+    var v = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
 79+    var f = TypeConverter_1.TypeConverter.firstValueAsNumber(factor);
 80+    if (v * f < 0) {
 81+        throw new Errors_1.NumError("Parameters of MROUND must have same signs (both positive or both negative).");
 82+    }
 83+    if (f === 0) {
 84+        return 0;
 85+    }
 86+    return Math.round(v / f) * f;
 87+};
 88+exports.MROUND = MROUND;
 89diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 90index eec1248..5ad0f58 100644
 91--- a/src/Formulas/AllFormulas.ts
 92+++ b/src/Formulas/AllFormulas.ts
 93@@ -68,7 +68,8 @@ import {
 94   PRODUCT,
 95   QUOTIENT,
 96   UPLUS,
 97-  UMINUS
 98+  UMINUS,
 99+  MROUND
100 } from "./Math";
101 import {
102   NA,
103@@ -335,5 +336,6 @@ export {
104   ISTEXT,
105   ISLOGICAL,
106   ISNUMBER,
107-  ISNONTEXT
108+  ISNONTEXT,
109+  MROUND
110 }
111\ No newline at end of file
112diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
113index 6bb9b93..9adad14 100644
114--- a/src/Formulas/Math.ts
115+++ b/src/Formulas/Math.ts
116@@ -1294,6 +1294,26 @@ var UMINUS = function (value) {
117 };
118 
119 
120+/**
121+ * Rounds a number to the nearest integer multiple of another.
122+ * @param value - value to round.
123+ * @param factor - multiple.
124+ * @returns {number}
125+ * @constructor
126+ */
127+var MROUND = function (value, factor) {
128+  ArgsChecker.checkLength(arguments, 2, "MROUND");
129+  var v = TypeConverter.firstValueAsNumber(value);
130+  var f = TypeConverter.firstValueAsNumber(factor);
131+  if (v * f < 0) {
132+    throw new NumError("Parameters of MROUND must have same signs (both positive or both negative).");
133+  }
134+  if (f === 0) {
135+    return 0;
136+  }
137+  return Math.round(v / f) * f;
138+};
139+
140 export {
141   ABS,
142   ACOS,
143@@ -1364,5 +1384,6 @@ export {
144   PRODUCT,
145   QUOTIENT,
146   UPLUS,
147-  UMINUS
148+  UMINUS,
149+  MROUND
150 }
151\ No newline at end of file
152diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
153index 6799527..2d3dd5d 100644
154--- a/tests/Formulas/MathTest.ts
155+++ b/tests/Formulas/MathTest.ts
156@@ -68,7 +68,8 @@ import {
157   PRODUCT,
158   QUOTIENT,
159   UPLUS,
160-  UMINUS
161+  UMINUS,
162+  MROUND
163 } from "../../src/Formulas/Math";
164 import * as ERRORS from "../../src/Errors";
165 import {
166@@ -78,6 +79,19 @@ import {
167 } from "../Utils/Asserts";
168 
169 
170+test("MROUND", function(){
171+  assertEquals(MROUND(4.12121, 22), 0);
172+  assertEquals(MROUND(10, 0), 0);
173+  assertEquals(MROUND(10, 2), 10);
174+  assertEquals(MROUND(21, 14), 28);
175+  catchAndAssertEquals(function() {
176+    MROUND.apply(this, [10]);
177+  }, ERRORS.NA_ERROR);
178+  catchAndAssertEquals(function() {
179+    MROUND.apply(this, [10, 1, 1]);
180+  }, ERRORS.NA_ERROR);
181+});
182+
183 test("GAMMALN", function(){
184   assertEquals(GAMMALN(4.5), 2.453736570842444);
185   assertEquals(GAMMALN(3), 0.6931471805599443);
186diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
187index afd89c8..b7422de 100644
188--- a/tests/SheetFormulaTest.ts
189+++ b/tests/SheetFormulaTest.ts
190@@ -671,6 +671,10 @@ test("Sheet ISNUMBER", function(){
191   assertFormulaEquals('=ISNUMBER(5)', true);
192 });
193 
194+test("Sheet MROUND", function(){
195+  assertFormulaEquals('=MROUND(21, 14)', 28);
196+});
197+
198 test("Sheet *", function(){
199   assertFormulaEquals('= 10 * 10', 100);
200   assertFormulaEquals('= 10 * 0', 0);