commit
message
Adding Formula.ROUND
author
Ben Vogt <[email protected]>
date
2017-02-08 04:44:17
stats
2 file(s) changed,
31 insertions(+),
1 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index ca2b8f1..63940ea 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -203,7 +203,24 @@ var __COMPLEX = {
6 var FISHER = Formula["FISHER"];
7 var FISHERINV = Formula["FISHERINV"];
8 var IF = Formula["IF"];
9-var ROUND = Formula["ROUND"];
10+
11+/**
12+ * Rounds a number to a certain number of decimal places according to standard rules.
13+ * @param values[0] The value to round to places number of places.
14+ * @param values[1] The number of decimal places to which to round.
15+ * @returns {number}
16+ * @constructor
17+ */
18+var ROUND = function (...values) {
19+ checkArgumentsAtWithin(values, 1, 2);
20+ var n = firstValueAsNumber(values[0]);
21+ if (values.length === 1) {
22+ return Math.round(n);
23+ }
24+ var d = firstValueAsNumber(values[1]);
25+ return Math.round(n * Math.pow(10, d)) / Math.pow(10, d);
26+};
27+
28 var ROUNDDOWN = Formula["ROUNDDOWN"];
29 var ROUNDUP = Formula["ROUNDUP"];
30 var SPLIT = Formula["SPLIT"];
31diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
32index c496deb..12806f6 100644
33--- a/tests/FormulasTest.ts
34+++ b/tests/FormulasTest.ts
35@@ -965,7 +965,20 @@ catchAndAssertEquals(function() {
36 }, ERRORS.NA_ERROR);
37
38
39+// Test ROUND
40 assertEquals(ROUND(99.44, 1), 99.4);
41+assertEquals(ROUND(99.44, 0), 99);
42+assertEquals(ROUND(99.4444444444444, 9), 99.444444444);
43+assertEquals(ROUND(99.44), 99);
44+assertEquals(ROUND("99.44"), 99);
45+assertEquals(ROUND([99.44, 22.222], [1, 4]), 99.4);
46+catchAndAssertEquals(function() {
47+ ROUND();
48+}, ERRORS.NA_ERROR);
49+catchAndAssertEquals(function() {
50+ ROUND(99.44, 1, 44);
51+}, ERRORS.NA_ERROR);
52+
53
54 assertEquals(ROUNDDOWN(99.46, 1), 99.4);
55