commit
message
Adding Formula.ROUNDUP
author
Ben Vogt <[email protected]>
date
2017-02-10 00:19:13
stats
2 file(s) changed,
34 insertions(+),
0 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 12dd20e..37ea38e 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -237,7 +237,24 @@ var ROUNDDOWN = function (...values) {
6 var d = firstValueAsNumber(values[1]);
7 return Math.floor(n * Math.pow(10, d)) / Math.pow(10, d);
8 };
9-var ROUNDUP = Formula["ROUNDUP"];
10+
11+/**
12+ * Rounds a number to a certain number of decimal places, always rounding up to the next valid increment.
13+ * @param values[0] The value to round to places number of places, always rounding up.
14+ * @param values[1] (optional) The number of decimal places to which to round.
15+ * @returns {number}
16+ * @constructor
17+ */
18+var ROUNDUP = function (...values) {
19+ checkArgumentsAtWithin(values, 1, 2);
20+ var n = firstValueAsNumber(values[0]);
21+ if (values.length === 1) {
22+ return Math.ceil(n);
23+ }
24+ var d = firstValueAsNumber(values[1]);
25+ return Math.ceil(n * Math.pow(10, d)) / Math.pow(10, d);
26+};
27+
28 var SPLIT = Formula["SPLIT"];
29 var SQRTPI = Formula["SQRTPI"];
30 var SUMIF = Formula["SUMIF"];
31diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
32index bd01673..48d1eb1 100644
33--- a/tests/FormulasTest.ts
34+++ b/tests/FormulasTest.ts
35@@ -1001,7 +1001,22 @@ catchAndAssertEquals(function() {
36 }, ERRORS.VALUE_ERROR);
37
38
39+// Test ROUNDUP
40 assertEquals(ROUNDUP(99.46, 1), 99.5);
41+assertEquals(ROUNDUP(99.99, 1), 100);
42+assertEquals(ROUNDUP(99.5555555555555, 9), 99.555555556);
43+assertEquals(ROUNDUP(99.99), 100);
44+assertEquals(ROUNDUP("99.99"), 100);
45+assertEquals(ROUNDUP([99.46666, 22.222], [1, 4]), 99.5);
46+catchAndAssertEquals(function() {
47+ ROUNDUP();
48+}, ERRORS.NA_ERROR);
49+catchAndAssertEquals(function() {
50+ ROUNDUP(99.44, 1, 44);
51+}, ERRORS.NA_ERROR);
52+catchAndAssertEquals(function() {
53+ ROUNDUP(99.999, "str");
54+}, ERRORS.VALUE_ERROR);
55
56
57 // Test SIN