commit
message
Added Formulas.TRUNC
author
Ben Vogt <[email protected]>
date
2017-02-16 01:52:34
stats
2 file(s) changed,
44 insertions(+),
3 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index fbceab8..a9c24ef 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -139,7 +139,6 @@ var SUMPRODUCT = Formula["SUMPRODUCT"];
6 var SUMSQ = Formula["SUMSQ"];
7 var SUMX2MY2 = Formula["SUMX2MY2"];
8 var SUMX2PY2 = Formula["SUMX2PY2"];
9-var TRUNC = Formula["TRUNC"];
10 var YEARFRAC = Formula["YEARFRAC"];
11
12 /**
13@@ -148,7 +147,7 @@ var YEARFRAC = Formula["YEARFRAC"];
14 * @returns {string} concatenated string
15 * @constructor
16 */
17-var CONCATENATE = function (...values) {
18+var CONCATENATE = function (...values) : string {
19 checkArgumentsAtLeastLength(values, 1);
20 var string = '';
21 for (var i = 0; i < values.length; i++) {
22@@ -164,6 +163,29 @@ var CONCATENATE = function (...values) {
23 return string;
24 };
25
26+/**
27+ * Truncates a number to a certain number of significant digits by omitting less significant digits.
28+ * @param values[0] The value to be truncated.
29+ * @param values[1] [ OPTIONAL - 0 by default ] - The number of significant digits to the right of the decimal point to
30+ * retain. If places is greater than the number of significant digits in value, value is returned without modification.
31+ * places may be negative, in which case the specified number of digits to the left of the decimal place are changed to
32+ * zero. All digits to the right of the decimal place are discarded. If all digits of value are changed to zero, TRUNC
33+ * simply returns 0.
34+ * @returns {number} after truncation
35+ * @constructor
36+ */
37+var TRUNC = function (...values) : number {
38+ checkArgumentsAtWithin(values, 1, 2);
39+ var n = firstValueAsNumber(values[0]);
40+ var digits = 0;
41+ if (values.length === 2) {
42+ digits = firstValueAsNumber(values[1]);
43+ }
44+ var sign = (n > 0) ? 1 : -1;
45+ return sign * (Math.floor(Math.abs(n) * Math.pow(10, digits))) / Math.pow(10, digits);
46+};
47+
48+
49 export {
50 __COMPLEX,
51
52diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
53index 6ff0be1..af7d3d8 100644
54--- a/tests/FormulasTest.ts
55+++ b/tests/FormulasTest.ts
56@@ -1329,7 +1329,25 @@ assertEquals(TANH([1, "str"]), 0.7615941559557649);
57 // Test TRUE
58 assertEquals(TRUE(), true);
59
60-assertEquals(TRUNC(3.1415, 2), 3.14);
61+
62+// Test TRUNC
63+assertEquals(TRUNC(PI(), 2), 3.14);
64+assertEquals(TRUNC("3.141592653589793", "2"), 3.14);
65+assertEquals(TRUNC(PI(), 1), 3.1);
66+assertEquals(TRUNC(PI(), 0), 3);
67+assertEquals(TRUNC(PI(), false), 3);
68+assertEquals(TRUNC(PI(), -1), 0);
69+assertEquals(TRUNC(31.41592653589793, -1), 30);
70+assertEquals(TRUNC([31.41592653589793], [-1]), 30);
71+assertEquals(TRUNC(31111.41592653589793, -4), 30000);
72+assertEquals(TRUNC(31111.41592653589793, -2), 31100);
73+catchAndAssertEquals(function() {
74+ TRUNC();
75+}, ERRORS.NA_ERROR);
76+catchAndAssertEquals(function() {
77+ TRUNC(3.1, 1, 1);
78+}, ERRORS.NA_ERROR);
79+
80
81 // Test XOR
82 assertEquals(XOR(1, 1), false);