commit
message
Added Formulas.DDB
author
Ben Vogt <[email protected]>
date
2017-02-19 17:03:34
stats
2 file(s) changed,
81 insertions(+),
9 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index b1f5aba..865dfaa 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -107,7 +107,6 @@ var DATEVALUE = function (dateString: string) : Date {
6 var DAY = Formula["DAY"];
7 var DAYS = Formula["DAYS"];
8 var DAYS360 = Formula["DAYS360"];
9-var DDB = Formula["DDB"];
10 var DEVSQ = Formula["DEVSQ"];
11 var DOLLAR = Formula["DOLLAR"];
12 var DOLLARDE = Formula["DOLLARDE"];
13@@ -129,6 +128,59 @@ var __COMPLEX = {
14 };
15 var YEARFRAC = Formula["YEARFRAC"];
16
17+
18+/**
19+ * Calculates the depreciation of an asset for a specified period using the double-declining balance method.
20+ * @param values[0] cost - The initial cost of the asset.
21+ * @param values[1] salvage - The value of the asset at the end of depreciation.
22+ * @param values[2] life - The number of periods over which the asset is depreciated.
23+ * @param values[3] period - The single period within life for which to calculate depreciation.
24+ * @param values[4] factor - [ OPTIONAL - 2 by default ] - The factor by which depreciation decreases.
25+ * @returns {number} depreciation of an asset for a specified period
26+ * @constructor
27+ */
28+var DDB = function (...values) : number {
29+ ArgsChecker.checkLengthWithin(values, 4, 5);
30+ var cost = TypeCaster.firstValueAsNumber(values[0]);
31+ var salvage = TypeCaster.firstValueAsNumber(values[1]);
32+ var life = TypeCaster.firstValueAsNumber(values[2]);
33+ var period = TypeCaster.firstValueAsNumber(values[3]);
34+ var factor = values.length === 5 ? TypeCaster.firstValueAsNumber(values[4]) : 2;
35+
36+ if (cost < 0) {
37+ throw new CellError(ERRORS.NUM_ERROR, "Function DDB parameter 1 value is "
38+ + cost + ". It should be greater than or equal to 0.");
39+ }
40+ if (salvage < 0) {
41+ throw new CellError(ERRORS.NUM_ERROR, "Function DDB parameter 2 value is "
42+ + salvage + ". It should be greater than or equal to 0.");
43+ }
44+ if (life < 0) {
45+ throw new CellError(ERRORS.NUM_ERROR, "Function DDB parameter 3 value is "
46+ + life + ". It should be greater than or equal to 0.");
47+ }
48+ if (period < 0) {
49+ throw new CellError(ERRORS.NUM_ERROR, "Function DDB parameter 4 value is "
50+ + period + ". It should be greater than or equal to 0.");
51+ }
52+ if (period > life) {
53+ throw new CellError(ERRORS.NUM_ERROR, "Function DDB parameter 4 value is "
54+ + life + ". It should be less than or equal to value of Function DB parameter 3 with "+ period +".");
55+ }
56+ if (salvage >= cost) {
57+ return 0;
58+ }
59+
60+ var total = 0;
61+ var current = 0;
62+ for (var i = 1; i <= period; i++) {
63+ current = Math.min((cost - total) * (factor / life), (cost - salvage - total));
64+ total += current;
65+ }
66+ return current;
67+};
68+
69+
70 /**
71 * Calculates the depreciation of an asset for a specified period using the arithmetic declining balance method.
72 * @param values[0] cost - The initial cost of the asset.
73@@ -145,10 +197,7 @@ var DB = function (...values) : number {
74 var salvage = TypeCaster.firstValueAsNumber(values[1]);
75 var life = TypeCaster.firstValueAsNumber(values[2]);
76 var period = TypeCaster.firstValueAsNumber(values[3]);
77- var month = 12;
78- if (values.length === 5) {
79- month = Math.floor(TypeCaster.firstValueAsNumber(values[4]));
80- }
81+ var month = values.length === 5 ? Math.floor(TypeCaster.firstValueAsNumber(values[4])) : 12;
82 if (cost < 0) {
83 throw new CellError(ERRORS.NUM_ERROR, "Function DB parameter 1 value is "
84 + cost + ". It should be greater than or equal to 0.");
85diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
86index 50e3d92..76faf86 100644
87--- a/tests/FormulasTest.ts
88+++ b/tests/FormulasTest.ts
89@@ -682,13 +682,23 @@ catchAndAssertEquals(function() {
90 DB(100, 50, 10, 2, 13);
91 }, ERRORS.NUM_ERROR);
92 catchAndAssertEquals(function() {
93- DB(100, 50, 10, 12, 13);
94+ DB(100, 50, 10, 12, 2);
95 }, ERRORS.NUM_ERROR);
96 catchAndAssertEquals(function() {
97 DB(100, -50, 10, 2, 12);
98 }, ERRORS.NUM_ERROR);
99
100+
101+// Test DDB
102 assertEquals(DDB(100, 50, 10, 2, 2.25), 17.4375);
103+assertEquals(DDB(100, [50], 10, 2, "2.25"), 17.4375);
104+catchAndAssertEquals(function() {
105+ DDB(100, 50, 10, 12, 2.25);
106+}, ERRORS.NUM_ERROR);
107+catchAndAssertEquals(function() {
108+ DDB(100, -50, 10, 2, 12);
109+}, ERRORS.NUM_ERROR);
110+
111
112 // Test DEC2BIN
113 assertEquals(DEC2BIN([100]), "1100100");